Destroying after animation in code

Hi I tend not to trust code to fire off when things can change outside of scripts, I’m not to sure how fragile animation events are though.

Here’s an alternative that destroy after the animation is over in code, make sure you place this script on the canvas (parent) to the damage text and that the animation is also on that object:

namespace RPG.UI
{
    public class DestroyAfterAnimation : MonoBehaviour
    {
        void Start()
        {
            Destroy(gameObject, GetComponent<Animation>().clip.length);
        }
    }
}

Edit:

In a later lecture you may run into an issue like I have where you can’t change the canvas scale so I had to create the prefab with an empty parent game object, to make this code still work with that change:

Destroy(transform.parent.gameObject, GetComponent<Animation>().clip.length);

Privacy & Terms