How to refer child game objects' instance in Unity

· 1 min read
How to refer child game objects' instance in Unity

Image you have a grouped objects in the hierarchy, Game object 1 has child game objects 1_0, 1_1 and 1_2 as following:

So how do we get the child game objects 1_0, 1_1 or 1_2 from game object 1? Suppose we have a script attached to game object 1 - GameObject1.cs:

private GameObject go0;
private GameObject go1;
private GameObject go2;

void Start() {

    // get game object 0
    go0 = gameObject.transform.GetChild(0).gameObject;
	
    // get game object 1
    go1 = gameObject.transform.GetChild(1).gameObject;

    // get game object 2
    go2 = gameObject.transform.GetChild(2).gameObject;

}