- Home /
Yield Continuous Death
Hello all, I'm trying to get my brain back into Unity and C# after a hiatus and could use your help! When my player falls to his death and collides with a box collider, this IEnumerator is triggered. The only problem is that livesLost is updating to remove more than one life (as if it is being triggered continuously). I thought the 1 second yield would prevent that, but I guess I'm thinking incorrectly. Any help on how to fix this code to assure my player only loses one life per plummet would be greatly appreciated!
private IEnumerator DieAnim ()
{
CoinScript livesLost = this.GetComponent<CoinScript> ();
livesLost.UpdateLives (1);
iAmDying = true;
humanDeath.Play ();//plays the death sound
animator.SetInteger ("AnimState", 1);
yield return new WaitForSeconds (1);//Use the length of the animation clip as the wait time for yield
Application.LoadLevel (Application.loadedLevel);//reload the current level
}
This part of script does waht you said exactly, maybe the code that triggers this part of code has problems, post that part of script too.
Thanks for the input. Here's the part of the code that triggers the IEnumerator:
} else if//if the player collides with destroyer
(collision.gameObject.tag == "Destroyer") {
Platformer2DUserControl stopmove = GetComponent<Platformer2DUserControl> ();
Destroy (stopmove);
StartCoroutine ("DieAnim");
}
Answer by Ibzy · Jul 04, 2015 at 11:39 PM
Sounds to me like the event is being triggered continuously when he hits the deck (constant collision).
I'd look to adding an isDead bool and checking for that in your if statement. I'd set it to true at the start of the coroutine and then false after Application.LoadLevel().
Hi, Ibzy and thanks for the input. With your suggestion along with changing the way I'm updating lives it seems to be working...now on to triggering the "Game Over" screen correctly when my player's lives dip below zero :D
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
OnTriggerEnter fails to activate 1 Answer
OnTriggerEnter firing twice 1 Answer
Enemy line of sight using linecast and colliders 1 Answer
Rigidbody Trigger 1 Answer