Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Delete after animation

Discussion in 'Scripting' started by D-007, Jul 11, 2022.

  1. D-007

    D-007

    Joined:
    Sep 14, 2021
    Posts:
    70
    I am having an issue with deleting an object after the animation ends. I am using a behaviour script on the object to be deleted, and using the function
    Code (CSharp):
    1.  override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    That unity creates automatically. In this function, I call
    Code (CSharp):
    1. Destroy(animator.gameObject);
    and the object is deleted after the animation ends.

    However I have an issue with my animator. Before the object is deleted after the destroy animation plays, the Entry node in the animator starts again, and therefore starts the flash animation. This only happens for a split second, but its obvious and can be seen. Does anyone possibly know a way I could fix this issue?


    upload_2022-7-11_20-42-31.png
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Can't you just remove the arrow connecting destroy to Exit? I'm pretty sure if it doesn't go to Exit, it won't restart the animations.
     
  3. D-007

    D-007

    Joined:
    Sep 14, 2021
    Posts:
    70
    I had tried that but the destroy animation just kept repeating itself.

    [edit] Just tried this suggestion again, and the animation stops on the last frame, and the object does not get deleted
     
    Last edited: Jul 11, 2022
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    Ah, I see. You're using OnStateExit, so you have to actually leave the animation state for it to be called. May not be the best solution, but can you just link it to a blank animation state?
     
    D-007 likes this.