本文介绍了删除GameObject的最后一个孩子的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的脚本,该脚本获取GameObject的子代数,然后销毁最后一个子代(我希望它基本上起删除键的作用),但出现错误:Can't remove RectTransform because Image (Script) depends on it.有人可以告诉我如何解决这个问题吗?

I'm trying to write a simple script that gets the child count of a GameObject and then destroys the last child (I want it to basically function like a delete key) but I'm getting the error: Can't remove RectTransform because Image (Script) depends on it. Can someone tell me how to resolve this?

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class DeleteSymbol : MonoBehaviour, IPointerClickHandler
{
    public GameObject deleteButton;
    public GameObject encodePanel;
    public GameObject decodePanel;

    #region IPointerClickHandler implementation

    public void OnPointerClick (PointerEventData eventData)
    {
        int numChildren = encodePanel.transform.childCount;             // get child count
        Debug.Log("There are " + numChildren + " children");

        if (numChildren > 0)
        {
            Destroy(encodePanel.transform.GetChild(numChildren - 1));       // destroy last child
        }
    }
    #endregion
}

推荐答案

解决了这个问题:

销毁(encodePanel.transform.GetChild(numChildren-1).gameObject);

Destroy(encodePanel.transform.GetChild(numChildren - 1).gameObject);

这篇关于删除GameObject的最后一个孩子的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 06:20