AnimationEvent.objectReferenceParameter

I’m trying to assign objectReferenceParameter to the gameObject that owns the script with the target function that I want to fire from the AnimationEvent.

    private void AssignAnimationEvents()
    {
        AnimationClip[] clips = mAnimator.runtimeAnimatorController.animationClips;
        for (int i = 0; i < clips.Length; ++i)
        {
            AnimationClip clip = mAnimator.runtimeAnimatorController.animationClips[i];
            AnimationEvent[] events = clip.events;
            for (int j = 0; j < events.Length; ++j)
            {
                AnimationEvent animEvt = clip.events[j];
                if (animEvt != null)
                {
                    animEvt.objectReferenceParameter = gameObject;
                    string funcName = animEvt.functionName;
                    // TODO: Use some reflection to assert function existence
                }
            }
        }
        mAnimator.Rebind();
    }

    public void End_Die()
    {
        Dbg.Log("End Die!!!!");
    }

However, after calling Rebind(), the function End_Die() still doesn’t fire. Is there something I’m missing here?

Okay, I figured this one out.

I misunderstood AnimationEvent.objectReferenceParameter to be the Object assigned that will receive the function call, functionName.

This is not the case. objectReferenceParameter is a user-defined object that can be used by the receiver, as in floatParameter, stringParameter, and intParameter.

So, with that cleared up, an event receiver must be a Component on the gameObject that has the Animator Component assigned.

2 Likes

Thanks a bunch, I was doing the exact same as you…

I also have made the same exact mistake all this time. I thought the “object” was the gameObject with the script which contains the function. Then I looked at the AnimationEvent class

and was confused as to why the example code wasn’t assigning to this anywhere.