4 Easy Ways to Get Child Objects In Unity
Nesting objects under another object is very useful for many situations, such as making an object’s position relative to another object. When you want to get child objects in Unity to run some functions on them all at the same time, you will need some ways to access those nested objects. Fortunately, Unity provides multiple ways of accessing child objects.
The simplest way to get a child object of a game object in Unity is to use the Find method of the Transform class, i.e. transform.Find(“Child Object’s Name”). This method will return the target child object which you can then perform various things with. If the object with the specified name doesn’t exist, the method will return null.
Unity provides various ways of accessing child objects such as getting a child object by name, getting a child object by its order within the parent object, or even getting all children of an object. Below, you will find various ways to get child objects in Unity.
Table of Contents
- How to Get A Child Object of An Object by Name in Unity
- How to Get A Child Object of An Object by Its Index in Unity
- How to Get Child Objects of An Object in Unity
- How to Call A Method In All Child Objects of An Object in Unity
How to Get A Child Object of An Object by Name in Unity
If you know the name of the child object you want to access, using the Transform.Find method is the simplest way to access the target child object.
Let’s say we have a game object with multiple child objects:
Now, if you want to get the object named “Child02” that is nested under the parent object named “TheParent“, call the Find method of the Transform component of its parent.
The example below assumes the script is attached to the parent object (TheParent object):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
void Start()
{
Transform child = transform.Find("Child02");
Debug.Log(child.name);
}
}
Code language: C# (cs)
Because the Transform component of the parent object is already made available within the MonoBehaviour script that’s attached to it, we use “transform” (all lowercase) instead of “Transform” (capitalized).
This makes it so we can call transform.Find right away without having to get the component with the GetComponent method first.
The example above will print out the name of the child object to the console.
Note that this method only works when you know the name of the child object you want. Also, inputting a name of a child object that doesn’t exist will result in the method returning a null value.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
void Start()
{
Transform child = transform.Find("ChildABCDEFG12345");
Debug.Log(child);
}
}
Code language: C# (cs)
This method also cannot be used to get a child object of another parent. To do that, you must have a reference to that parent’s Transform component.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
[SerializeField]
private Transform anotherObject;
void Start()
{
Transform childOfAnotherObject = anotherObject.Find("ChildA");
}
}
Code language: C# (cs)
How to Get A Child Object of An Object by Its Index in Unity
Sometimes, it’s not always ideal to get child objects by their names if they’re procedurally generated and there are a bunch of them under a parent object.
This is where we get them by the index of their positions instead. This is similar to dealing with arrays.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
void Start()
{
Transform child = transform.GetChild(0);
Debug.Log(child.name);
}
}
Code language: C# (cs)
The example above will print out the name of the child object at index number 0 which is the first child in the hierarchy.
Note that the GetChild method will throw an out-of-bound error if you specify an index that’s bigger than the total number of all the children under the parent.
To combat this, use the childCount property to determine the total number of child objects of the parent object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
void Start()
{
Transform child = transform.GetChild(transform.childCount - 1);
Debug.Log("Child Count: " + transform.childCount);
Debug.Log(child.name);
}
}
Code language: C# (cs)
The example above gets the last child object of this parent object and prints out its name to the console. The reason we have to subtract one from the count is because the index starts at 0, not 1.
You might notice that the child count returns 4 when there are 5 child objects under the parent object. This is because it only counts the direct child objects.
How to Get Child Objects of An Object in Unity
There are times when you want to access child objects of an object. Unfortunately, Unity doesn’t have an easy method to use to get a list of all children nested under a game object.
But not all hope is lost. We can work around this issue by iterating through all of the child objects using a for-each loop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetChildrenScript : MonoBehaviour
{
void Start()
{
List<Transform> children = new();
foreach (Transform child in transform)
{
Debug.Log(child.name);
children.Add(child);
}
Debug.Log("Count: " + children.Count);
}
}
Code language: C# (cs)
It is not documented, but when a Transform is used with a for-each loop, you can loop through all of the direct child objects of that Transform.
The example above will get all of the child objects and store them in a List.
How to Call A Method In All Child Objects of An Object in Unity
There will come a time when you want to execute a command for all of the child objects within a parent object. For example, when you want to iterate through all children of an object and make them execute a function like shoot bullets or explode all at the same time.
Unity has made it quite easy to do this. All you have to do is call the BroadcastMessage method.
This is the parent’s script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParentObject : MonoBehaviour
{
void Start()
{
transform.BroadcastMessage("SayMyName");
}
private void SayMyName()
{
Debug.Log("My name is " + transform.name);
}
}
Code language: C# (cs)
And this script below is attached to each of the child objects:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildObject : MonoBehaviour
{
private void SayMyName()
{
Debug.Log("My name is " + transform.name);
}
}
Code language: C# (cs)
What BroadcastMessage does is it will execute the method with the specified name in all of the MonoBehaviour scripts of all child objects AND the parent object itself, if there is one.
The BroadcastMessage method can also take a parameter and pass it to the specified method. For more information, see the official documentation.
This is useful when you just want to execute a method of child objects without having to first get them.
Attribution
Diagram icon made by Good Ware from www.flaticon.com