How do I run an animation then destroy a game object?
I'm trying to code a scenario where after a zombie is killed, it runs an animation (name of the animation is "back_fall"), then waits for a couple seconds, then it is destroyed. Here is my current code:
var EnemyHealth : int = 100; var Zombie : GameObject;
function DeductPoints (DamageAmount : int) {
EnemyHealth -= DamageAmount;
}
if( EnemyHealth <= 0 ){ GameObject.Find("Zombie").animation.Play("back_fall"); Destroytimer (); }
function Destroytimer (){ yield WaitForSeconds (animation["back_fall"].length); Destroy(gameObject); }
NOT PART OF CODE: for some reason not all of my code went into the little boxes (sorry). The console says "Type 'UnityEngine.Component' does not support slicing.".
Answer by ComradeVanti · Mar 25, 2016 at 10:20 PM
What I would do is create a public void function that simply starts a Coroutine witch waits your desired time and than destroy the object.
Then add an event to the last frame of your animation. This event should call the void function. Done.
If you are not familiar with animationevents and coroutines check these links:
Coroutines: http://docs.unity3d.com/Manual/Coroutines.html
Events: http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html