How to remove last sprite in Animation once played

Hi,

I can’t seem to remove the final sprite of an animation once it has been played - any ideas on the below?

Here is my code for where the animation plays on enemy death (deathEffect is the animation);

    void Die()
    {

    ScoreScript.scoreValue += 100;
    Instantiate(deathEffect, transform.position, Quaternion.identity);
    Destroy(gameObject);
    }

You can see a gif of this problem in action here;

As you can see, when the enemy is dead, the animation plays and then doesn’t disappear off screen.

Hi,

You are not performing any removal in your code, you just instantiate an animation which plays automatically (I assume.)

You could destroy/disable your explosion after the animation has played. To achieve this, You could use a Coroutine to do this (wait inside the coroutine, then destroy/disable after a delay.) You could also use invoke.

Another way to do this would be to use Unity’s animation state machine system (Mecanim) and then just change state when the animation is over.

And I think you could also use animation events to call a function when the last frame has played.

1 Like

Hi Olmi,

Thanks a lot for this, your idea helped a lot and got what I wanted :slight_smile: