Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deactivate all children in Unity?

Tags:

c#

unity3d

How do I only deactivate all children in unity and leaving the parent active?

like image 879
AuroraBlaze Avatar asked Mar 09 '14 05:03

AuroraBlaze


People also ask

How do you remove a child from Unity?

Method 1: Remove From Hierarchy Click on HubGuyPink and drag the GameObject to an empty space in the Hierarchy. Both parent and child will become independent GameObjects again. Easy peasy. Deleting the child GameObject will also make the parent GameObject independent.


2 Answers

foreach (Transform child in transform)
    child.gameObject.SetActive(false);
like image 136
Roberto Avatar answered Oct 25 '22 12:10

Roberto


//Assuming parent is the parent game object
for (int i = 0; i < parent.transform.childCount; i++)
{
    var child = parent.transform.GetChild(i).gameObject;
    if (child != null)
        child.SetActive(false);
}
like image 38
David Avatar answered Oct 25 '22 13:10

David