Anyway to search "just:" the children of a game ob

I have used the GameObject.Find() command and know how to use the find with the “/” letter for hierarchies. My question is: is there some way to just search the children of an object for a object by name?

I ask this because I have children named “ShadowBlob” of many main objects. I don’t want to find all the “ShadowBlob” objects, I just want to find the one attached to a particular main object in the main object’s script. Is there some way to do this?

Hello :slight_smile:

Yes, find the root transform with GameObject.Find, then do something like this:

foundGameObject : GameOBject = GameObject.Find("yourGOName");
for( var child : Transform in foundGameObject){
    Debug.Log(child.name);
}

That for goes trough all the children of your found gameObject and prints their names :slight_smile:

hope it helps you out :wink:

That does help, thank you. Now, how do you convert from a Transform to a GameObject? I was able to retrieve the Transform before but that didn’t give me the info I needed from the actual GameObject.

You don’t do a conversion, really. You find the Game Object to which the Transform component is attached.

child.gameObject

Pretty easy! :smile:

GameObject.Find() searches all objects for a match but there’s actually an analogous Find() function you can call on any transform object to only search the children.

Looping over the children (unless you do a recursive loop) will only search one depth down. transform.Find() will search in all children.

I know this is an old thread, but are you 100% sure that the tranform.Find() will search recursively through all the children?