Thursday, 29 December 2016

How to Delete Canvas Panel Child Objects

1. You need to calculate the number of child objects
2. Use loops to get child one by one delete the using Destroy method.



Method 1 :-
========
if (InfoButtonPanel.transform.childCount >= 0) {
     Transform[] allChildren = InfoButtonPanel.transform.GetComponentsInChildren<Transform>();
foreach (var item in allChildren) {
Destroy (item.transform.gameObject);
}
}


Method 2 :-
========
if (InfoButtonPanel.transform.childCount >= 0) {
     for (int i = 0; i < InfoButtonPanel.transform.childCount; i++) {
    GameObject.Destroy(InfoButtonPanel.transform.GetChild(i).gameObject);
}
}

No comments:

Post a Comment