How to find child of a GameObject or the script attached to child GameObject via script

Finding child GameObject by index:

You can get the first child of a GameObject with the GetChild function.

GameObject originalGameObject = GameObject.Find("MainObj");
GameObject child = originalGameObject.transform.GetChild(0).gameObject;

You can get other children by providing index number of the child GameObject such as 1,2,3, to the GetChild function.

and if its a child of a child in the original gameobject? would I just have to repeat it or would it just be the nth one down

You find the child first, then find the child of that child from the reference.

Let’s say this is OriginalGameObject/Prefab hierarchy:

  • OriginalGameObject
  • child1
  • child2
  • child3
    • childOfChild3

As you can see the OriginalGameObject is the parent of child1, child2 and child3. childOfChild3 is the child of child3.

Let’s say you want to access the childs and you only have reference to OriginalGameObject which is the parent GameObject:

//Instantiate Prefab
GameObject originalGameObject  = Instantiate(prefab);

//To find `child1` which is the first index(0)
GameObject child1 = originalGameObject.transform.GetChild(0).gameObject;

//To find `child2` which is the second index(1)
GameObject child2 = originalGameObject.transform.GetChild(1).gameObject;

//To find `child3` which is the third index(2)
GameObject child3 = originalGameObject.transform.GetChild(2).gameObject;

The index starts from 0 so the real index number is index-1 just like arrays.

Now, to get the reference of childOfChild3 which is the child of child3 but you only have reference to OriginalGameObject which is the parent GameObject:

First of all, get reference of child3 then get childOfChild3 from it.

GameObject mychild = originalGameObject.transform.GetChild(2).gameObject;
GameObject childOfChild3 = mychild.transform.GetChild(0).gameObject;

Finding [all] child GameObject by index with loop:

To loop through all the child of originalGameObject:

GameObject originalGameObject = Instantiate(prefab);
for (int i = 0; i < originalGameObject.transform.childCount; i++)
    GameObject child = originalGameObject.transform.GetChild(i).gameObject;
    //Do something with child

You can also find child by name with the transform.FindChild function. I wouldn’t recommend that you can do that. It seems slow and will conflict when multiple child share the-same name. That’s why you use GetChild.

Finding child GameObject by name:

GameObject child1 = originalGameObject.transform.FindChild("child1").gameObject;
GameObject child2 = originalGameObject.transform.FindChild("child2").gameObject;
GameObject child3 = originalGameObject.transform.FindChild("child3").gameObject;

To find childOfChild3, you can easily do that with the ‘/‘ just like you do with a file directory. You provide the parent/child then name. The parent of childOfChild3 is child3. So, we use, childOfChild3/child3 in FindChild function.

GameObject childOfChild3 = originalGameObject.transform.FindChild("child3/childOfChild3").gameObject;

Finding scripts/components attached to child GameObject:

If all you want is the script that is attached to the child GameObject, then use GetComponentInChildren:

MyScript childScript = originalGameObject.GetComponentInChildren<MyScript>();

If there are more than one child with the-same script and you just want to get all the scripts attached to them, then use GetComponentsInChildren:

MyScript[] childScripts = originalGameObject.GetComponentsInChildren<MyScript>();
for (int i = 0; i < childScripts.Length; i++)
    MyScript myChildScript = childScripts[i];
    //Do something with myChildScript

Leave a Comment