Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    57
    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,381
    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:
    57
    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,381
    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.