So the issue is I would like the death animation to play before the player actually dies THEN goes to game over… But what it’s doing is bypassing the death animation and just executing destroy and game over…
Things I’ve tried are. WaitForSeconds and taking out the Destroy line… And these haven’t worked…
Does anyone else know a timer system or their code method that will allow the whole death animation to play the WHOLE way through and THEN execute the Destroy and game over, please???
if (playerHealth == 0)
{
anim.SetBool(“isDead”, true);
Destroy(collision.gameObject);
gameOver();
You can say Destroy(collision.gameObject, 1f) and it won’t destroy for 1 second. Or you can use an AnimationStateController or an animation event that will notify your game object that it should destroy itself.
If you need to wait to do 2 things (destroy and call gameOver), then you can either use Invoke to call a method that does these two things or use a coroutine.
It’s not “bypassing” the death animation, it’s never getting a chance to run it. Animations require time, and zero time passes before you delete the object that’s meant to play the animation.
There’s a few ways you could do this.
One is to put all of that in the Update() method, and use an internal timer variable. Advance it using Time.deltaTime, and once it’s above certain values then do certain things. This works and is simple, but fiddly.
My preference would be for a coroutine. That uses the WaitForSeconds thing you mentioned, which is really useful. It can’t be used in a normal function though, only a coroutine. There you can write a normal-looking funciton, call it with StartCoroutine(…), and in the function use “yield return new WaitForSeconds(aNumber);” at the parts where you want to wait.
It is also possible to create a “SelfDestruct” MonoBehavior (that waits for N seconds, and then destroys GameObject to which it is attached), and attach it to gameObject in question.
The issue with coroutines is that they don’t serialize which makes live debugging slightly more complicated.
Unfortunately if anyone here has mastered the art of divination they’re yet to share it with me.
What I’m saying, in a silly way, is that when you say something “didn’t work” you’re not giving us any useful information to help you with. When it “doesn’t work”, what happens? Nothing? An error message in the console? A compile error? The object still disappears immediately? Your computer turns itself off?
Set the “isDead” boolean to “true” in your Animator.
Request that Unity destroys your GameObject one second from now.
Call the “gameOver” function.
The issue is that while the object destruction is queued to happen one second from now, the gameOver() function’s contents are not. The code doesn’t pause until the Destroy(…) call has done its thing (and for delayed actions like that you don’t want it to).
Now… is that what you’re doing? If so, check out the solutions suggested in the posts above. If I have guessed correctly at your problem I’m pretty sure any of them can work. If you have trouble applying any of them, come back and let us know in detail what you’ve done and what the results are.