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 }