Control GameObjects Using Hierarchy Structure in Unity

Control GameObjects Using Hierarchy Structure in Unity

In Unity, it's common to manage GameObjects by manually assigning them in the Inspector to a list or array. However, this process can become tedious and error-prone, especially when you deal with many objects.

A more efficient and flexible approach is to create an empty parent object and organize all child GameObjects under it. Then you can access them programmatically from your script without having to assign each one manually.

Looping with Transform, Not GameObject

Here's one crucial point: to loop through children, you should use Transform, not GameObject.

This is because the parent-child relationship in Unity is not managed by GameObject itself, but by its Transform component.

  • GameObject holds general data like name, tag, and attached components.
  • Transform manages the position, rotation, scale, and the hierarchy structure (parent/children).

Conceptually, Unity's internal Transform class can be visualized like this:


class Transform
{
    public Vector3 position;
    public Quaternion rotation;
    public Vector3 scale;

    public Transform parent;
    public List<Transform> children;

    // Other methods...
}

Because the Transform class contains both parent and children references, you can use a loop like foreach (Transform child in parent) to iterate through children.

Hierarchy Example and Code

Let’s say you have the following object structure in your Unity scene:

  • StarGroup (parent GameObject)
  •     ↳ Star1
  •     ↳ Star2
  •     ↳ Star3
Control GameObjects Using Hierarchy Structure in Unity
public class StarManager : MonoBehaviour
{
    public Transform starGroup;
    public List<Transform> starList = new List<Transform>();

    void Start()
    {
        foreach (Transform child in starGroup)
        {
            starList.Add(child);
        }
    }
}

With this setup, you only need to assign StarGroup in the Inspector. All child objects will be automatically added to the list in the script. Make sure the variable type is Transform, not GameObject.

Transform.GetChild()

This syntax is actually implemented by internally calling Transform.GetChild(int index) repeatedly. In other words, foreach (Transform child in parent) is simply a convenient shorthand that loops through all child objects by automatically calling GetChild(i) behind the scenes.

This makes the code much cleaner and more concise, but functionally it's equivalent to writing a for loop like this:

for (int i = 0; i < transform.childCount; i++)
{
    Transform child = transform.GetChild(i);
    Debug.Log(child.name);
}

Therefore, if you need to access child objects in a specific order or iterate in reverse, using a for loop is often more suitable.

Conclusion

Unity manages its hierarchy structure through the Transform component. Once you understand this, you can build more maintainable and cleaner code by simply organizing your objects properly in the Hierarchy.

Especially when working with a large number of objects, this approach helps you avoid repetitive Inspector assignments and keeps your project easy to manage.

Popular posts from this blog

Understanding Arrays as Reference Types in C#

Setting Up a Basic Follow Camera with Cinemachine 3.x

Understanding and Using ? (nullable) in C#