How to get the instance of a child in a prefab object?

Hello,

I would like to get children inside an instantiated prefab with the name of each children.

But If I have instantiate several prefabs I can’t use the method GameObject.Find()

Prefab1

  • Child1
  • Child2

Prefab2

  • Child1
  • Child2

Thanks.

EDIT: I think I have found how to:

GameObject go = transform.FindChild("the_name").gameObject;

Is it the good method?

Thanks.

Instantiate() returns a reference to the cloned object. You should probably cache that reference if you’ll want to use it later.

Here’s a contrived example:

public class Example : MonoBehaviour {
    public GameObject prefab; //set this in the inspector
    
    GameObject clone;
    
    void Start() {
        clone = Instantiate(prefab) as GameObject;
        Invoke("DoSomething", 2f); //call this two seconds from now
    }
    
    void DoSomething() {
        clone.transform.localScale *= 2f; //double the size, double the fun!
    }
}

If prefab has any child GameObjects, the clone will have matching child clones.