- Home /
Using animation events on instantiated prefabs.
I am instantiating the prefabs at runtime and do not know how to call a function on the instantiated prefab in the scene as I cannot place the reference to the object it needs to call the function on.
Hope one of you can help :)
Answer by FM-Productions · May 22, 2017 at 05:48 AM
Hi,
in your prefab, simply attach your script as component to the main gameObject. Also make sure the Animator component is on the same gameObject. I think that Animation Events triggered from an animator component should call the function of a script that is attached to the same gameObject as the Animator itself.
If I interpreted something wrong, please provide more details about your specific problem (where is the animatio event called, how does your prefab look etc.).
Answer by Skaster87 · May 24, 2017 at 07:41 AM
Hi,
I'm not exactly sure if this is what your asking, but here's how I store a reference for an object that's being instantiated:
GameObject anObject = Instantiate(prefab, position, rotation);
Var e = anObject.GetComponent<TYPE>();
Answer by Fik-shun · Jul 01, 2020 at 09:07 PM
For future visitors, you might want to check the 2nd example here: https://docs.unity3d.com/ScriptReference/AnimationEvent.html,For future visitors, yo might want to check the 2nd example here: https://docs.unity3d.com/ScriptReference/AnimationEvent.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612122310im_/https://answers.unity.com/themes/thub/images/avi.jpg)