AI death scripting problem

For some reason when I shoot an enemy and their health is reduced to zero, the death animation won’t play.

var player : GameObject; var speed :
float = 1;
static var health :int = 30;

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

animation["death"].wrapMode = WrapMode.ClampForever;

if (!player)
Debug.Log ("ERROR could not find Player!");

}
function Update() { if (!player) return;


var distance = Vector3.Distance( player.transform.position,

transform.position);

if ( distance < 60 )
{

var delta = player.transform.position -

transform.position;
delta.Normalize();

var moveSpeed = speed * Time.deltaTime;

transform.position = transform.position + (delta *

moveSpeed);
}

if ( distance < 60 )
transform.LookAt(player.transform);

if (moveSpeed > 0.1)
animation.CrossFade("walk");
else
animation.CrossFade("idle");

if ( distance < 10)
animation.CrossFade("attack");

if ( health < 1 ){
animation.Play("death");
speed = 0;

Debug.Log ("Dead");

}

}



function OnTriggerEnter( otherObject: Collider ){ 

if(otherObject.gameObject.tag == "bullet"){

health -= 5;


}

}

Any help with why this is happening would be great, thanks :slight_smile:

try to add

animation.Stop();

in your Start() function. This stops all animations and rewind them. Then all your animations is set to its starting frame. Does “Dead” print out, by the way?