Search Unity

How to remove last sprite in Animation once played

Discussion in 'Animation' started by dathraneu, Aug 15, 2020.

  1. dathraneu

    dathraneu

    Joined:
    Jul 17, 2020
    Posts:
    24
    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);

    Code (CSharp):
    1.     void Die()
    2.     {
    3.  
    4.     ScoreScript.scoreValue += 100;
    5.     Instantiate(deathEffect, transform.position, Quaternion.identity);
    6.     Destroy(gameObject);
    7.     }
    You can see a gif of this problem in action here;
    https://gyazo.com/2e6848817b59bd339c2f0415ce0e526e

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

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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.
     
    dathraneu likes this.
  3. dathraneu

    dathraneu

    Joined:
    Jul 17, 2020
    Posts:
    24
    Hi Olmi,

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