Destroy an object after an animation

Hi,
So I have an object called goblin, when its health is empty, I want it to play an death animation and then to be destroyed.
But when I put that in the script, it plays the animation in loop, even when I tell it to play it once…
If somebody coul help me because I’m really stuck…

Here is the script :

using UnityEngine;
using System.Collections;

public class attackGoblin : MonoBehaviour {




    Animation anim;
    public int currentHealth;
    public int dead = 1;
    bool isDead;                                            
    bool damaged;

    public void stop()
    {
        currentHealth = currentHealth - 20;
    }

    public void walk()
    {
        anim["attack1"].wrapMode = WrapMode.Once;
        anim.Play("attack1");


    }



    void Start(){
        anim = GetComponent<Animation> ();
        currentHealth = 100;
       
    }




    void Update(){
        var goblin = GameObject.Find("GOBLIN");
        var creature = GameObject.Find("creature1");
        var distance = Vector3.Distance(goblin.transform.position, creature.transform.position);
        //Debug.Log(distance);
        if (currentHealth > 0)
        {
            if (!anim.IsPlaying("attack1"))
            {
                Debug.Log(currentHealth);



                if (distance < 2)
                {

                    anim.Play("combat_idle");
                }

                if (distance >= 2)
                {

                    anim.Play("idle");
                }
            }
           

        }
        else if(currentHealth<=0)
        {
            anim["death"].wrapMode = WrapMode.Once;
            anim.Play("death");
            if (!anim.IsPlaying("death"))
            {
                Destroy(goblin);
            }
        }

    }
}

I usually work with mecanim animator, so I might not be the best of help for the looping issue.

But if we bypass that issue, you could add an animation event on the last frame of your dead animation. That animation event would call a function in your goblin to delete the object.

I’m a beginner so I’m gonna try to do that but if there is an easier way I take it haha

Thank you for your answer ! :slight_smile:

Read this if you need help :

https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

But I didn’t see you had the code for the death animation in your post.

Now I know why you have a loop :

You keep calling it.

Every time update loop, you start the dead animation. So as soon as your animation finish, it will start again, BEFORE checking if the animation is over.

Is there a reason why you dont use the animator controller?

You could just use a trigger to call it once, then set an animation event to call a function to destroy your goblin.

1 Like

It’s just that I began the project with a 3D model that didn’t have an animator controller ^^
But I found the solution, I added the animation death in the void stop and put"if (!anim.IsPlaying(“death”))" in the update section so he wasn’t able die again (I don’t know if I explain it right haha)

But I’m gonna check how to use the animator, thank you !

I understand how you did it, it’s fine. :slight_smile: It’s a simple way to achieve it without using animator controller.

If you feel alright using the legacy animation system (using anim.play ect…) I don’t think it’s an issue. I think a lot of people dislike the new animator controller and prefer to keep using the legacy.

I want to build a game for june so it will be more efficient to use the animator I think. But it seems to be two really different ways to animate so I will follow a few tutorials to be sure.