unity get child game objects code example

Example 1: unity get number of child objects

// The number of child objects is given by:
GameObject.transform.childCount;

Example 2: how to get an child of an gameobject

// By Name
GameObject Child = GameObjectsTransform.Find("NameOfChild").gameObject

// By index
GameObject Child = GameObjectsTransform.GetChild(The child index).gameObject

Example 3: unity get child

GameObject Child;
Child = transform.GetChild(0).gameObject;

Example 4: unity get all children

foreach (Transform child in parent) {
  //Your code
}

Example 5: unity get child gameobject

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

//To find `child1` which is the first index(0)
GameObject child2 = 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;