Skip to content

[Unity] How to Get the Number of Child Game Objects

Last Updated on 2021-10-31 by Clay

The different objects in Unity may have a parent-child hierarchical relationship, and if we want to confirm how many child objects there are under an objects, we can use the follow program to do it:

gameObject.transform.childCount


It should be noted that when writing gameObject, it represents the game object that the script attached.

In fact, you can use the following code to count any GameObject's child object number.

For example:

public GameObject World;

void start() {
    print(World.transform.childCount);
 }

You can count how many child objects there are under the object World.

As for why it is necessary to obtain the number of child objects? Sometimes it may be just control the memory usage, and sometimes it may be adjusted for the rendering of the scene, both of which are very important tasks.


References


Read More

Tags:

Leave a Reply