Destroy object after death animation.

Hi people,

I’m new here and i’ve 1 question about destroying a object
after playing the death animation.

the code:

void Die()
{
if (isDead)
return;
PlayAnimation(GlobalSettings.animDeath1, WrapMode.ClampForever);
this.CurrentHp = 0;
isDead = true;
Destroy(gameObject);

}
}

But now the object (npc) destroyed without playing the death animation.

:face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes::face_with_spiral_eyes:

Use this if you know how long your animation last, before call Destroy(gameObject):

yield WaitForSeconds (5);
destroy(GameObject);

change “5” for hw long for ur animation end, then destroy ur object.
Hope it help.

I have do this:

void Die()
{
if (isDead)
return;
PlayAnimation(GlobalSettings.animDeath1, WrapMode.ClampForever);
this.CurrentHp = 0;
isDead = true;

yield WaitForSeconds (5);
destroy(GameObject);

}
}

But then i get a error: Unexpected symbol (', expecting )‘, ,', ;’, [', or =’
Sorry i’m a beginner in cs scripting.

aahh… so, are you using C#?
well the Code are a little differ. To you use Coroutine, you must make a IENumerator method to use yield. (sorry about my bad english):

void Update() {
   StartCoroutine(Example()); //this will run your timer
}

IEnumerator Example() {
  yield return new WaitForSeconds(5); //this will wait 5 seconds 
}

If you dont understand, or get erro, post here again, then i try to explain in other words. Unity scripting may help you alot to understand the classes, methods, functions…

Best regards

1 Like

THANKS!!!THANKS!!!THANKS!!!THANKS!!!THANKS!!!THANKS!!!THANKS!!!THANKS!!!THANKS!!!
:smile::smile::smile::smile::smile::smile::smile::smile::smile::smile:
I searched a solution for 5 days and now it works.

For people who have this problem too:

IEnumerator Example() {
if (CurrentHp <= 0)
{
yield return new WaitForSeconds(5); //this will wait 5 seconds
Destroy(gameObject);
}
}

void Die()
{
if (isDead)
return;
PlayAnimation(GlobalSettings.animDeath1, WrapMode.ClampForever);
this.CurrentHp = 0;
isDead = true;
Example();
}
}

And this on the update void:

StartCoroutine(Example()); //this will run your timer

Just as a quick addition, and because I believe it could be helpful as another solution. If you see the documentation in destroy http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html you’ll notice that Destroy can accept two parameters. first is the GameObject that needs to be destroyed, and second (optional) the time the engine should wait before destroying the object. And since the given solution works for you, you might consider using
Destroy(gameObject, 5);

and avoid using the yield instruction.

A better solution that takes into account the length of the animation clip.

private int currentHealth;

public void TakeDamage(int damage)
{
    currentHealth -= damage;
    if (currentHealth <= 0)
    {
        StartCoroutine(Die());
    }
}

private IEnumerator Die()
{
    PlayAnimation(GlobalSettings.animDeath1, WrapeMode.ClampForever);
    yield return new WaitForSeconds(gameObject, GlobalSettings.animDeath1.length);
    Destroy(gameObject);
}

The code you have has StartCoroutine in Update which isn’t good because it will kick off a new coroutine every frame.

hm, I’ld better use animation event instead. Just press ctrl+d on death animation clip, select it, drop to animator controller or animation, then on animation tab select die animation. scroll it to the end. make animation event. set function there which will do destroy gameobject.
I’m still not get used to IEnumerators with their yield syntaxis.

Animation events didn’t exist 5+ years ago when this thread was ongoing…

@KelsoMRK

could you please explain this more in detail as I am having an issue with a coroutine atm please see…
my issue here

OR
private float time = 1.2f;

void Update() {
if (healthCount <= 0) {
Destroy(gameObject, time); //Will destroy the gameObject if health is or less than “0” after 1.2 seconds
}
}

For people looking for an easy solution in 2022, the best way is to use the “add behavior” button from your death animation node in the animator controller window. From that script, you put Destroy(animator.gameObject) in the method that triggers after the transition from the death animation to the next node, which could be an empty node in most cases.

1 Like