Hi,
I’m dynamically creating an instance of a prefab. The prefab has code attached to it. Inside the prefab I have an animated mesh (A character walking) called “body”
How can I get my C# code to reference the “body” inside that instance, and only that instance?
If I use:
pBody = GameObject.Find("body");
I often get the body attached to a different prefab instance.
I hit google for quite while but no luck.
Thanks!
While instantiating the prefab, you can assign it to a variable, depending on what it is. Then you just look up its animation component:
Transform pBody = (Transform)Instantiate(body, Vector3.zero, Quanternion.identity);
It would be wiser to have the character control itself, though (since you already have a script in the prefab, you can make the pre-fab autonomous from the spawning entity)
Sounds like you want Transform.Find instead.
GameObject b = transform.Find("Body"); // returns child GO named Body
GameObject c = GameObject.Find("Body"); //finds a GO in the world named Body