- Home /
Game randomly subtracts 2 lives when it is only supposed to subtract 1
In my game, objects fall towards the bottom of the screen and the player has to shoot them out. In Lives mode, if they pass the bottom of the screen, 1 life is supposed to be subtracted and all other objects one the screen are destroyed to sort of reset and give the player a second.
Every once in awhile it will subtract 2 lives instead of just 1.
This Update checks if an object has gone past the bottom of the screen.
void Update () {
isColliding = false;
Vector2 pos = transform.position;
pos = new Vector2 (pos.x, pos.y - speedY * Time.deltaTime);
transform.position = pos;
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2 (0,0));
if(transform.position.y < min.y - 0.5){
if(!isFalling){
if(ClickType.gameType == 2){
gameObject.GetComponent<AudioSource>().clip = oops;
gameObject.GetComponent<AudioSource>().Play();
}
isFalling = true;
}
StartCoroutine(OhNo());
}
}
This is what performs when an object does go past the screen
IEnumerator OhNo(){
yield return new WaitForSeconds(0.8f);
Destroy(gameObject);
if(ClickType.gameType == 2){
GameObject[] fallingShapes = GameObject.FindGameObjectsWithTag("FallingShape");
foreach(GameObject fS in fallingShapes){
GameObject.Destroy(fS);
}
Timer.livesRemaining -= 1;
Debug.Log("Lives are:" + Timer.livesRemaining);
}
}
Any and all help would be much appreciated. Thanks very much!
Answer by FortisVenaliter · May 04, 2017 at 07:43 PM
When the object goes below the bottom of the screen, you are starting a new coroutine in the Update function, e.g. every frame that it still exists below the screen.
Since the coroutine destroys the object immediately after running, in most cases it will only actually run the code after WaitForSeconds once. But the Destroy command kills the object at the end of the frame, not right away. And since floating points can be imprecise, it's possible that on uncommon occurences, two will finish in the same frame, effectively running it twice. To confirm this, add a Debug.Log() to the coroutine function before WaitForSeconds. It will likely log about 48 times per block (0.8 * 60FPS).
What you probable want to do is have a class-scope boolean variable called 'isDestroying' or something. Check that it's false before starting the coroutine, and set it to true after you start the coroutine the first time. That will ensure it's only being called once.
Thank you @FortisVenaliter. I did the Debug test and you were right. I then added a new bool and it only came up once, fixing the issue. It is funny I have used this bool technique before with collisions, and thought my isFalling bool was taking care of this, but it appears I needed another. Thanks again!
Well, it may have, but your StartCoroutine call is not within the isFalling block.
Yes. Looking back that was actually to prevent the audio cue from playing each frame. Thanks again
Your answer
Follow this Question
Related Questions
WaitForSeconds using Update? 1 Answer
Multiple coroutines starting from Update() freeze/crash Unity 0 Answers
Stuck while solving a Coroutine problem 0 Answers
How can I make this only act once? 3 Answers
Change Bool When Audio Clip Ends C# 1 Answer