r/unity_tutorials • u/comp-3_interactive • Apr 26 '20
A little script to automatically destroy a game object once it's animation is complete
4
u/baz4tw Apr 26 '20
I like things like this.
Does that ‘require component animator’ bit at the top mean you can’t place this script unless an animator is attached to the object first?
4
u/comp-3_interactive Apr 26 '20
Kind of, but If you attach this to an object without an Animator then the editor automatically attaches one and refuses to let you remove it while the script is attached. Is a really useful attribute to help with setting up assets and also negating any "component not found" errors
3
2
u/Sumruv Apr 26 '20
Cant you also call scripts from the animation? So you can have something like destroy(gameobject) in a script that is called at the end. Are there disadvantages to that?
2
u/brenananas Apr 26 '20
You could call a function that destroys the game object with an animation event, yes. In that case, the destruction of the animated object would be "baked" into the animation, whereas OP's solution would allow you to decide on a per-instance basis whether or not the object should be destroyed at the end by including or excluding the script. Additionally, if you had multiple objects with different animations that should be destroyed, you'd have to key in the animation event for each one, which might be more tedious than just attaching this component.
The upside of using the animation event would be that you have more control over which animation triggers the destruction - for example, if you had an enemy that should persist and run through several other animations until its health reaches 0, and then destroy it at the end of its death animation. With OP's script as-is, the enemy would be destroyed as soon as its first animation concludes. So, depends on your use case, really. OP's script seems to be more suited for fire-and-forget situations, like VFX. Say you had a sprite-based explosion animation; all you'd have to do is spawn it and then you can forget about it. It would run through its animation once and handle destroying itself at the end
1
2
Apr 27 '20 edited Apr 27 '20
I believe Animancer allows setting callbacks for animations. It's so frustrating that we are still relying on methods like this. What if I want to slow / speed up an animation? Or just change animation. I have written a fair bit of code to manage my animations and I really don't see how you could make a semi animation heavy game without one..
2
1
u/RailgunZx Jun 22 '20
This was posted a month ago but in case you're reading I wanted to let you know that I just scrolled back through all my saved posts just to find this so that I can use it right now!
Although my usage was a little different, I'm using the animation length in a co-routine to perform an action at the end of an animation without having to mess with animator events
1
u/MortgagePlastic2539 Jan 16 '26
6 years later your still saving games. Thx man you really saved me so much time.
6
u/comp-3_interactive Apr 26 '20
We can do this by taking advantage of the animators state information. We grab the length of the animation at the 0 index and appended any delay we require. Pop that number into the second parameter of the Destroy method and we're all set! Frame perfect destruction