How to disable all children in Unity?

How to disconnect only all children in unity and leave the parent active?

+4
source share
3 answers
//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);
}
+3
source
foreach (Transform child in transform)
    child.gameObject.SetActive(false);
+2
source

Try:

public void DisableChildren()  
{    
    foreach (Transform child in transform)     
    {  
        child.gameObject.SetActiveRecursively(false);   
    }   
}

JS version (if necessary):

function DisableChildren()   
{   
    for (var child : Transform in transform)    
    {   
        child.gameObject.SetActiveRecursively(false);  
    }  
}
0
source

Source: https://habr.com/ru/post/1530800/


All Articles