Remove Animation event

Hi,
I use AddEvent on animation clip to add few AnimationEvents for a specific model.
Then I load a different scene, which have a copy of the same model.
It appears that the new model’s animation still contain the events added to a different model in a different scene.
This model doesn’t have the same Components attached to it, so I get an Exception saying that AnimationEvent has no receiver.

Isn’t this a bug? Shouldn’t all animation event added by code should be remove when changing scenes?
If not, is there a way to remove animation events, so I’ll be able to remove all animation events I added before continuing to the next scene?

Thanks,
Yinon

Please can you post the script where you add the events?

Here the method that adds the event
The HasClipEvent and AddClipEvent calls are just used to keep track of the names of the event added so I wont add the same event twice

:

private void setupAnimation(string animationName, int layer, string functionName, string functionParam, float eventTime)
    {
        animation[animationName].layer = layer;
        if(functionName != null   AnimEventHandlers.HasClipEvent(animation[animationName].clip,functionName) == false)
        {
		    AnimationEvent animEvent = new AnimationEvent();
		    animEvent.functionName = functionName;
            if(functionParam != null)
            {
		        animEvent.stringParameter = functionParam;
            }
            float localEventTime = eventTime;
            if(eventTime == -1)
            {
                localEventTime = animation[animationName].clip.length * 0.9f;
            }
		    animEvent.time = localEventTime;
            animation[animationName].clip.AddEvent(animEvent);
            AnimEventHandlers.AddClipEvent(animation[animationName].clip, functionName);
            //print("Added animation event: anim= " + animationName + " time = " + animEvent.time + " functionName = " + animEvent.functionName + "param = " + animEvent.stringParameter);
        }
        
    }

It turns out that this has already been logged as a bug (it is case number 230246 if you want to follow it up) but it hasn’t been fixed yet.

Do we have an update to this bug? I’m trying to add an animation event which only triggers the first time and then removes itself. How would you recommend I do this, particularly in such a way that I can trigger a one-shot event going either forwards or backwards in the animation. My application is to use it as a trigger to stop looping sounds with enough of a margin before the animation completion that they have a little while to fade out.

Thanks so much!

+1, is there any update on this annoyance ?

As a workaround, I ended up creating my own looping sound support and just putting repeating animation events at the start and end of animations. Did the trick.

You can remove events like this:

public static void RemoveEventFromAnimator(string functionName, Animator animator)
{
AnimatorClipInfo animationClipInfo = animator.GetCurrentAnimatorClipInfo (0) [0];
AnimationClip animationClip = animationClipInfo.clip;

AnimationEvent[ ] animationEvents = animationClip.events;
List updatedAnimationEvents = new List ();

for (int i=0; i<animationEvents.Length; i++) {
AnimationEvent animationEvent = animationEvents*;*

  • if(animationEvent.functionName != functionName){*

  • updatedAnimationEvents.Add(animationEvent);*

  • }*

  • }*

  • animationClip.events = updatedAnimationEvents.ToArray ();*

  • }*

3 Likes