Search Unity

AnimationEvent help - Solved

Discussion in 'Scripting' started by Thunder0ne, Mar 22, 2009.

  1. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
  2. Charles Gill

    Charles Gill

    Joined:
    Aug 1, 2008
    Posts:
    142
    What this means that that your AnimationEvent is trying to call back with a function name that doesn't exist.

    You can supress this error by flagging the event like this;

    myEvent.messageOptions = SendMessageOptions.DontRequireReceiver;

    However it implies the function name you specified in your AnimationEvent is missing.

    Does this help?

    Charles
     
  3. Charles Gill

    Charles Gill

    Joined:
    Aug 1, 2008
    Posts:
    142
    One more thing;

    When the event is handled a SendMessage is used to call the function name you specify, and this function must exist in the same level in you hierarchy (or possibly lower), not in a component in a parent object, as AnimationEvent does not use SendMessageUpwards.

    Charles
     
    Whatever560 likes this.
  4. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
    Thanks for the help.
    please kindly find the source code here: it looks like the <<don't send message>> option does not fit my requirements, so I need to understand why the event handler is not recognised by the animation event
    Code (csharp):
    1.  
    2. var mySkin : GUISkin;
    3. var animatedGameObject : GameObject;
    4. var horOffset = Screen.width * 0.18;
    5. private var animationEvent : AnimationEvent;
    6.  
    7. //Solved: move this function to a script attached to the animatedGameObject
    8. function AnimationEnded(animEvent : AnimationEvent){
    9.  
    10.     animatedGameObject.animation.CrossFade ("idle");
    11.    
    12. }
    13.  
    14. function Start(){
    15.  
    16.     animationEvent = new AnimationEvent();
    17.     animationEvent.time = animatedGameObject.animation["attacco"].clip.length * 0.9;
    18.     //animationEvent.messageOptions = SendMessageOptions.DontRequireReceiver;
    19.     animationEvent.functionName = "AnimationEnded";
    20.     animatedGameObject.animation["animation1"].clip.AddEvent(animationEvent);
    21.     animatedGameObject.animation["animation2"].clip.AddEvent(animationEvent);
    22.    
    23. }
    24.  
    25. function OnGUI () {
    26.     GUI.skin = mySkin;
    27.  
    28.     if(GUI.Button (Rect (Screen.width * 0.0 + horOffset, Screen.height * 0.75,Screen.width * 0.18, Screen.height * 0.25),"")){
    29.    
    30.         animatedGameObject.animation.CrossFade ("animation1");
    31.        
    32.     }
    33.  
    34.     if(GUI.Button (Rect (Screen.width * 0.20 + horOffset, Screen.height * 0.75,Screen.width * 0.18, Screen.height * 0.25),"")){
    35.    
    36.         animatedGameObject.animation.CrossFade ("animation2");
    37.        
    38.     }
    39. }
    40.  
     
  5. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
    I got it now.
    The function handling the animation event must reside in a script attached to the animated object.
    I had not understood your suggestion in the first go.
    Thanks again.
     
  6. Benmaster

    Benmaster

    Joined:
    Aug 11, 2016
    Posts:
    39
    Hi, Im trying to make some animation events to work, first I try with this example:

    Code (CSharp):
    1.  
    2. public SimpleEventReceiver _EventReceiver;
    3. public AnimancerState activeState;
    4.         _EventReceiver.onEvent.Set(activeState, (animationEvent) =>
    5.         {
    6.             Debug.Log("_EventReceiver");
    7.         });
    8.  
    The I put in the animation some event called "Event" with no params (or params, is the same for the case)
    And I get an error "NullReferenceException: Object reference not set to an instance of an object." because dont recognize the "animationEvent" param for some reason.

    I try to create directly a public void function called "Event", in the same controller, and in this case is executed, so im not sure what im doing wrong.
     
    Last edited: Dec 11, 2019