How To Destroy Object After Death Animation Finishes?

I’ve looked at a bunch of similar threads but I can’t seem to find the solution. I want the death animation to play and then the player to get destroyed after the animation has finished playing. This is what I have so far:

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class playerHealth : MonoBehaviour
{
    //Variables
    float currentHealth;
 
    public void addDamage(float damage)
    {
        if(currentHealth<=0){
            StartCoroutine(Die());
        }
    }
 
    private IEnumerator  Die(){
        PlayAnimation(GlobalSettings.Player_Dead, WrapeMode.ClampForever);
        yield return new WaitForSeconds(gameObject, GlobalSettings.Player_Dead.length);
        Destroy(gameObject);
    }
}

I keep getting this error: Assets/Scripts/playerHealth.cs(17,10): error CS0305: Using the generic type System.Collections.Generic.IEnumerator<T>' requires 1’ type argument(s)

Thanks for the help, I’m new to c#.

I’ll take a guess that this line:

yield return new WaitForSeconds(gameObject, GlobalSettings.Player_Dead.length);

should be more like this:

yield return new WaitForSeconds(GlobalSettings.Player_Dead.length);

:slight_smile:

Thx for the try but It’s still giving the same error: Assets/Scripts/playerHealth.cs(17,10): error CS0305: Using the generic type System.Collections.Generic.IEnumerator<T>' requires 1’ type argument(s)

Sorry, my bad… I think you are using a type I haven’t used before.
One more try – try adding “using System.Collections” to the top of your script (namespaces area). :slight_smile:

I’ve tried that already too but decided to try it again. It still gives the same error it gave me last time. It gives a bunch of error similar to this: Assets/Scripts/playerHealth.cs(18,3): error CS0103: The name `PlayAnimation’ does not exist in the current context

OK, first, if Coroutines are almost never the best solution to a problem — certainly true if you’re new to Unity programming. They are convenient, but quickly lead you into a twisty maze of complications.

Second, if you do want to use a Coroutine, then you do indeed need to be #using System.Collections.

Third, you’re doing this very much the hard way. All you want is to destroy the object after a delay, right? The Destroy method has a built-in parameter for that. So delete the coroutine, and just change your AddDamage method to:

        if(currentHealth<=0){
            PlayAnimation(GlobalSettings.Player_Dead, WrapeMode.ClampForever);
            Destroy(gameObject, GlobalSettings.Player_Dead.length);
        }

That’ll do it!

Great answer for you, there. Destroy with its overload.

PlayAnimation not in that context is a different problem, altogether. Wasn’t even paying attention to that earlier. :slight_smile:

I still think I’m doing something wronge, this is my entire code right now:

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class playerHealth : MonoBehaviour
{
    //Variables
    public float fullHealth;
    public GameObject deathFX;
    public AudioClip playerHurt;
    float currentHealth;
    public Slider healthSlider;
    public Image Damage_Screen;
    bool damaged = false;
    float smoothColour = 5f;
    Color damagedColour = new Color(255f,24f,24f,0.5f);
    playerControl controlMovement;
    AudioSource playerAS;
    Animator ani;
   
    // Use this for initialization
    void Start ()
    {
        currentHealth = fullHealth;
        controlMovement = GetComponent<playerControl>();
        healthSlider.maxValue = fullHealth;
        healthSlider.value = fullHealth;
        damaged = false;
        playerAS = GetComponent<AudioSource>();
        ani = GetComponent<Animator>();
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (damaged){
            Damage_Screen.color = damagedColour;
        }else {
            Damage_Screen.color = Color.Lerp(Damage_Screen.color, Color.clear, smoothColour*Time.deltaTime);
        }
        damaged = false;
    }
   
    public void addDamage(float damage){
            if (damage<=0) return;
                currentHealth -= damage;
                playerAS.PlayOneShot (playerHurt);
                healthSlider.value = currentHealth;
                damaged = true;
               
            if(currentHealth<=0){
                PlayAnimation(GlobalSettings.Player_Dead, WrapeMode.ClampForever);
                Destroy(gameObject, GlobalSettings.Player_Dead.length);
        }
    }
}

and i’m getting four errors saying WrapeMode, GlobalSettings and PlayAnimation does not exist in context. Thx for the help :smile:

Well, it’s true. There’s no such thing as WrapeMode. Check the script reference for what you’re trying to use here, and compare it to the spelling in your actual code.

It’s also true that there is no global PlayAnimation method. Perhaps you mean to call that on the Animator component, like ani.PlayAnimation?

Finally, GlobalSettings is not a Unity thing — I assumed that was something you have in your project.

Not sure what you mean here but I found a way to make it work. I just replaced this:

            if(currentHealth<=0){
                PlayAnimation(GlobalSettings.Player_Dead, WrapeMode.ClampForever);
                Destroy(gameObject, GlobalSettings.Player_Dead.length);
        }

with this:

            if(currentHealth<=0){
                ani.SetBool("isDead",true);
                Destroy (gameObject, this.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length + [B](Delay)[/B]);
        }

This just adds a delay to the object being destroyed and i triggered the animation by just changing the “isDead” variable to true. Just change “Delay” to what fits you animation best. This might not be the best way but it works so I’m ok with it.