How to create animation event at specific keyframe at runtime


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();

   // 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) {
      else {

function myEventFunction () {

	print("Nice job event added");

I definitely have a problem with the line:


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 :


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;*

}_ = _aEvents;

This may help you. Thanks.

link text

,link text