Unity: Get Child by Name
Introduction
In Unity, beginner game developers frequently try to set up interactions between game objects by identifying these objects by name. Getting the object by name is a beginner-friendly approach that is straightforward for newcomers. So, this guide will explain how to find children by name.
However, I recommend against finding children by name.
Instead, you should use a Component to identify specific types of objects and then use GetComponentsInChildren to find things with that component. This approach is more reliable and robust. If you rename the child, the method still works. If you rename the component, the method still works.
Sometimes, you will want to get a child by name for quick prototyping or debugging. Fortunately, it is easy to do. In this guide, I’ll explain how to do it.
Find a child by name in Unity
Use Unity’s Transform.Find method. As the parameter, pass in the exact name of the child you want to find.
Transform child = transform.Find("childName");
Transform.Find()
only looks at the first level in the hierarchy. Sometimes, you want to find children in lower levels of the hierarchy. In the next section, I’ll explain how.
Find a grandchild by name in Unity
You have two options. Specify an exact path or iterate through the list of all children.
For example, assume your hierarchy looks like this:
- Parent
|─ Child
|─ Grandchild
Now imagine that you call transform.Find("Grandchild")
from the Parent. This call will return null.
Method 1: Specify the exact child path
In this method, you can specify the exact child path. As you can see, we use a “folder-like” slash to distinguish each path. You can use this as many times as you want.
transform.Find("Child/Grandchild");
In this method, you become dependent on more than one name. The immediate child as well as the grandchild. Specifying the exact child path is error-prone - even more so than using transform.Find()
alone. I recommend against this approach.
Method 2: Get all children, then match by name
The second method is more complex, but more robust. For that reason, I recommend it. In this approach, you get all children, then match them by name.
Transform[] transforms = GetComponentsInChildren<Transform>();
foreach (Transform t in transforms)
{
if (t.name == "Grandchild")
{
Debug.Log(t, t.gameObject);
}
}
Learn more about how to get all children of a transform.
Children vs. Parents
In Unity, a child belongs to a parent, and a parent can have more than one child. In this example, we have gone downwards in the hierarchy and gotten a component from a child. Sometimes, you need to go the other way and get a component from a parent. For that, you can use the GetComponentInParent method.
Conclusion
As you can see, getting a child by name in Unity is straightforward. To reinforce what I mentioned earlier, you should generally avoid this method. Instead, use components to identify GameObjects and filter with those components.
Continue brushing up your Unity skills with our Unity basics series: