Destroy game object after animation

OK so I've been looking into "Animation Events" not that I understand how they work but it might be what I need for my animation problem. I have a UFO that takes off from a hill when the player gets within a certain distance, but whay I want is about partway thought the fight animation for a small explosion to occure on the side of the UFO and then for it to explode an "Destroy game object" at the end of the animation cycle??

Here is my code so far:

var UFOsound : AudioClip;
var escapeDist = 0.4;
private var player : GameObject;

function Start(){
        player = GameObject.FindWithTag("Player");
}

function Update(){
        if(Vector3.Distance(transform.position, player.gameObject.transform.position) < escapeDist){
        animation.Play("UFOtakeoff");
        audio.PlayOneShot(UFOsound);
        }
}

Everything works great but I can't figure out how to get the small explosion to happen mid flight in the animation, I'm thinking animation event but I've no clue how that works, been looking for an example. Unity's code reference does not really explain it in a way I can understand.

So I guess I would need two animation events, one for the explosion mid flight and another event for at the end of the animation, that is if I understand that part corectly. If I could somehow code in a dead replacement that would be super cool too, but again, my coding knowlede is not that great yet, still trying to wrap my head around how a lot of it works... getting there though, slowly, Oh so slowly :)

To destory an object from the animation editor…

  1. Create a MonoBehaviour with a simple public function that will destroy the object. e.g.

    public class Destroyable : MonoBehaviour
    {
    public void DestroyMe() { Destroy(gameObject); }
    }

  2. Add that MonoBehaviour to the animated object you want to destroy.

  3. In the animation editor, move the animation scrubber to the end of the animation.

  4. Use the ‘Add Event’ button in the animation toolbar

  5. Select ‘DeleteMe’ from the function drop-down in the Edit Animation Event dialog.

Now your animation should play, run the ‘DeleteMe’ function, and destroy the object.

If you're working with imported animations, you can't add animation events using the animation editor since it's read only. What you can do is add the events in scripts instead.

function Start()
{
    var clip = animation["UFOtakeoff"].clip;

    // Explode 2 seconds into the clip.

    var explode = new AnimationEvent();
    explode.functionName = "ExpodeUFO";
    explode.time = 2.0f; 

    // Destroy at end of clip.

    var destroy = new AnimationEvent();
    destroy.functionName = "DestroyUFO";
    destroy.time = clip.length;

    clip.AddEvent(explode);
    clip.AddEvent(destroy);
}

function ExplodeUFO()
{
    // Create explosion here
}

function DestroyUFO()
{
    // Destroy UFO here
    Destroy(gameObject);
}