Search code examples
c#unity-game-enginegameobject

Is there a way to check if all children of an object are active?


I am a beginner C# coder, and I am trying to make a game in Unity. Hence the question: Can I check if all children of an object are active in a scene? I want to use it to check if all enemies are active.


Solution

  • You could check using:

    for (int i = 0; i< gameObject.transform.childCount; i++)
    {
        if(!gameoObject.transform.GetChild(i).gameObject.activeInHierarchy)
        {
            return false;
        }
    }
    return true;
    

    activeInHierarchy is exactly what you need.