Iterating through children of an object

I want to crawl down the hierarchy of an object. I thought this would work:

foreach (Transform t in gameObject.transform)

I could swear ive seen this being used successfully, and even used it myself in the past. But right now it’s only checking the object the script is attached to, and none of its children.

am i doing something wrong?

foreach (Transform child in transform)

should work.

Try to debug it:

Debug.Log(transform.childCount);

If it doesn’t help then please share your project.

That will only give you the direct children try something like this for the children of children etc

foreach( Transform child in GetComponentsInChildren<Transform>() )

Or create a recursive function to crawl through.

2 Likes

Doesn’t work.
transform.childCount reads as 1

Here’s my hierarchy, creature is where the script is

ding! that works. i wanted to read the entire hierarchy, for some reason i thought the original would do that

1 Like

Creature only has 1 child, GlitchMainASC2.