Animation doesn't play correctly

I have some code that should play a dying animation and then erase that game object(enemies in FPS shooter).I made the necessary animation states and transitions in Mecanim.I have a “isDead” trigger parameter to call the “Die” animation state(with a die clip).But the clip is playing not even half of the clip and looping this sequence,this resulting in a very weird animation sequence.If i call the “Destroy (gameObject)” after the animation, it doesn’t even try to play it, just erases the enemy.Anyway, here is my code:

 using UnityEngine;
    using System.Collections;
    
    public class EnemyHealth : MonoBehaviour {
    
    	Animator anim;
    	public int TheDammage;
    	int Health = 600;
    
    	// Use this for initialization
    	void Start ()
    	{
    		anim = GetComponent<Animator>();
    	}
    	
    	// Update is called once per frame
    	void Update () {
    
    	
    	}
    
    	void ApplyDammage ()
    	{
    		Health -= TheDammage;
    		
    		if(Health <= 0)
    		{	
    			anim.SetTrigger("isDead");
    			anim.Play("Die");
    			Destroy (gameObject);
    		}
    	}
    }

So this is “Die Hard”…without Bruce Willis.Can anyone help the poor troll to die in peace before disappearing?Thank you!

You are calling 3 different things the can’t interact together.

Setting the trigger will make mechanim follow its state machine, so there is no need to call
anim.Play(), that is mechanim’s job.

Destroy is called on the same frame, hence will destroy the object right away.

if you look at the scripting API, Unity - Scripting API: Object.Destroy

you’ll see you can call Destroy(object, float), which will only destroy the object after T seconds. (you can put the length of your animation there).

You can also add an animation event, or a behaviour to your state machine (if using U5) to do the job, instead of coding in the time it should wait before destroying.

OK, so i made it work.For everyone having this problem this is the solution:all its done in the animator - first i checked the “Has Exit Time” box of the transition Any State → Die.This forces the die animation to be played until the end(if this checkbox is unchecked it only plays the first frames and looping them).After checking the “Has Exit Time” checkbox i opened the Settings of this transition(Any State - Die) and unchecked the “Can Transition To” box.This forces the animation to be played only once.Of course in the code the boolean isDead it will be set to true and after that call the Destroy(gameObject,animation_playing_time_delay) so it removes the enemy after playing the whole dying animation.

Also:

Not trying to necro a dead thread for no reason, but there is something else to check for. I’m posting my solution because using Unity 2021, most of these answers still apply, BUT ALSO:

Make absolutely POSITIVE that if you are using multiple layers in your Animator (for example, a Base Layer and also a Top Body (“mask”) layer to ensure feet keep moving as your characters attack, that in the gear icon of all your active layers, the Weight setting is raised to 1. Apparently it must be 0 by default, and any animation layer that has a weight of 0 will play in the Animator window, but will effectively be invisible and look like it isn’t playing.

I wasted two or three hours trying to refactor code because of this (and because one of the first rules of design are “trust no one’s work, not even your own”). Hopefully this helps someone else who comes across this post from Google or another engine, because it was 2nd or 3rd on the list when I searched for “unity 3d calling animation from code doesn’t play.” Be well, fellow game designers!