aahh… so, are you using C#?
well the Code are a little differ. To you use Coroutine, you must make a IENumerator method to use yield. (sorry about my bad english):
void Update() {
StartCoroutine(Example()); //this will run your timer
}
IEnumerator Example() {
yield return new WaitForSeconds(5); //this will wait 5 seconds
}
If you dont understand, or get erro, post here again, then i try to explain in other words. Unity scripting may help you alot to understand the classes, methods, functions…
Just as a quick addition, and because I believe it could be helpful as another solution. If you see the documentation in destroy http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html you’ll notice that Destroy can accept two parameters. first is the GameObject that needs to be destroyed, and second (optional) the time the engine should wait before destroying the object. And since the given solution works for you, you might consider using
Destroy(gameObject, 5);
hm, I’ld better use animation event instead. Just press ctrl+d on death animation clip, select it, drop to animator controller or animation, then on animation tab select die animation. scroll it to the end. make animation event. set function there which will do destroy gameobject.
I’m still not get used to IEnumerators with their yield syntaxis.
For people looking for an easy solution in 2022, the best way is to use the “add behavior” button from your death animation node in the animator controller window. From that script, you put Destroy(animator.gameObject) in the method that triggers after the transition from the death animation to the next node, which could be an empty node in most cases.