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 :slight_smile:

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.).

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>();

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: Unity - Scripting API: AnimationEvent