Retrieve All Child Objects

I want to retrieve all child game objects from parent game object. For this requirement I search other posts but didn’t get any useful information.

So I create new post to correct my problem. Following image illustrate my problem correctly.

[22579-screenshot+2014-02-22+17.00.32.png|22579]

I have cell collection object in hand and through which I want to retrieve all the child objects. Although children have different tag attached so I can’t use GameObject.FindObjectByTag(“”);

So what is the efficient way to retrieve child game objects?

1 Answer

1

Maybe something like this would do what you want?

    // C#
    foreach (Transform child in transform)
    {
        // do something with child here
    }

You would use this on the parent object.

+1 This is the usual way to access the direct childs of a gameobject. However you have to be careful what you do inside the foreach loop. You can't destroy the childs in such a loop since that would change the collection you're iterating over. In general it's a good idea to include in the question why you want to do something because not all situations have the same solution ;) Also since Unity has 3 different languages (UnityScript, C#, boo) it would help to specify what language you use to get a more suited answer.

–

Right. and that's also not recursive. if you want to get all the children recursively, consider just gameObject.GetComponentsInChildren < Transform > ()

–

Thanks for your suggestion.

–