How to create animation event at specific keyframe at runtime

Hi!

I have a script which create an animation at runtime from an array of coordinates.
Now i want to add some events at specific keyframes positions of this animation clip.

here is the script for adding the events:

private var myEvent : AnimationEvent;
private var e : int=0; // place of the event in events[]

function AddEventToMyAnimation() {
	
   myEvent = new AnimationEvent();
   myEvent.functionName="myEventFunction";

   // I set keys for each animation frame
   // so the variable t represents the current frame position on my animation.
   for (var t : int=0; t<animationDataArray.Length; t++) {

      if (iWantAnEvent==true) {
		
         myAnimationClip.AddEvent(myEvent);
         myAnimationClip.events[e].time=t;
         e+=1;
      }
      else {
      }
   }
}

function myEventFunction () {

	print("Nice job event added");
}

I definitely have a problem with the line:

myAnimationClip.AddEvent(myEvent);

This line seems to crash Unity (close itself) when the script executes at runtime…

Since there’s not a lot of documentation about scripting animation events, I guess I’m doing something wrong. Unless it’s a real Unity bug issue!?

(docs : http://docs.unity3d.com/ScriptReference/AnimationClip.AddEvent.html)

Hii…

You want to add multiple events at specific time so you need to create array of animation events although you are calling same function. and specify time to each event.

    private AnimationEvent[] _aEvents;
    private Animator _myAnim;
 
 
    // Use this for initialization
    void Start () {
        _myAnim = GetComponent<Animator> ();
 
        _aniclip = _myAnim.runtimeAnimatorController.animationClips[0];
 
        _aEvents = new AnimationEvent[3];
        for (int i = 0; i < 3; i++){
            _aEvents *= new AnimationEvent();*

aEvents*.functionName = "myEventFunction ";
_aEvents.time = i;*

}_

_aniclip.events = _aEvents;

}
This may help you. Thanks.

link text

,link text