Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How could I remove an animation event?

Discussion in 'Scripting' started by JohnSonLi, Jun 2, 2015.

  1. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    no api like animationclip.removeEvent.....How ?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    There is no remove event function however it is possible to set/get the events with animationClip.events. So call the get, remove it from the array and then call the set.
     
    Last edited: Jun 3, 2015
  3. pokeking

    pokeking

    Joined:
    May 13, 2015
    Posts:
    42
    i use code like on this page: http://stackoverflow.com/questions/22896391/unity3d-move-a-sprite-when-pressing-a-key
    then i create animation, and set this in Update:
    Code (CSharp):
    1.         if (isMoving == false) {
    2.             GetComponent<Animator> ().speed = 0;
    3.            
    4.         } else {
    5.             GetComponent<Animator> ().speed = 1;
    6.         }
    mine is just stop animation when stop walking, so i don't know it can help you or not
    sorry for my English :D
     
    ZetroEUW likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    heeyoung_abc likes this.
  5. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @karl.jones I've been trying to put together a worked example of what you are suggesting but I've run into something of a roadblock with unity crashing intermittently and not being able to iterate over the events array returned from the get...

    <snip, whole bunch of not relevant stuff>


    also getting some very bizarre crashes but I think that's "me" rather than a problem with the script/setup... could someone else run the test and see if it works (in which case it's all "just me" lol o_O )
     
    Last edited: Jun 3, 2015
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    Seems like the event array contains null objects.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    yeah, but there are definately 5 events doing something because the functions are being called and the debugs are coming out o_O

    edit: still get the null exception even if I take out the parameter bit, just looking for functionName,

    that should be able to output 4 "SendSomethingToLog" and 1 "RemoveEvents" :(
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Can you upload your project or a simplified version? I can check if its a bug or something with your project.
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    <yoink not shared no more :p >

    i think you should be able to get it from there...
     
    Last edited: Jun 3, 2015
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Everything works for me.

    In fact I got events removing with this quick hack

    Code (csharp):
    1.  
    2. public void RemoveEvents()
    3. {
    4.     AnimatorClipInfo[] myClipInfos = anim.GetCurrentAnimatorClipInfo(0);
    5.     AnimationClip myCurrentClip = myClipInfos[0].clip;
    6.     AnimationEvent[] myEvents = myCurrentClip.events;
    7.     Debug.Log(myEvents.Length);
    8.  
    9.     if (myEvents.Length > 0)
    10.     {
    11.         var list = myEvents.ToList();
    12.         list.RemoveAt(0);
    13.         myCurrentClip.events = list.ToArray();
    14.     }
    15. }
    16.  
    What version of Unity are you using?
     
  13. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Unity 5.0.1f1
    didn't update to the latest version yet on this machine... good to know I was on the right path and it's "just me" :)
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Can you upgrade to 5.0.2f1, there is a fix you need.

    • Mecanim: Fixed crash when calling AnimationClip.events from script.
     
  15. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    aha, updated and it now works :) awesome thanks karl
     
    karl_jones likes this.
  16. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    wait a sec, where do you get these updated information for every version?
     
  17. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    The Release notes section Capture.PNG
     
  18. razzraziel

    razzraziel

    Joined:
    Sep 13, 2018
    Posts:
    362
    Since there is still no removal method, setting the event array to zero works fine.

    Code (CSharp):
    1. animationClip.events = new AnimationEvent[0];