- Home /
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.
Answer by rutter · Jan 18, 2014 at 01:45 AM
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.
Your answer
Follow this Question
Related Questions
How do I get the count of children in a prefab? 1 Answer
Make a simple tree 1 Answer
How do I get the global position 2 Answers
Update Parent/ Children From Script? 2 Answers
Affect All Instances Of A Prefab 3 Answers