- Home /
Each update runs my code 6 times per line. Is it a bug?
Ok, here is the deal... am trying to make an update so for each second I will add 1 point. My result is that each second I get 6 points... And why this happens is not because the update runs 6 times like a loop, it is because each line when in the update is running 6 times before going to the next line of code, and it is 6 times and so on.
private float timeLeft = 1.0f; void Update(){
timeLeft -= Time.deltaTime;
if(timeLeft == 0) //so each second I get inside my if statement...
{
//add score runs 6 times
ScoreCounter.add(1); //debug breakpoints hits here 6 times before going to next line bellow that resets the timer.
//reset timer runs 6 times
timeLeft = 1.0f;
}
}
My issue seems to be totally illogical to me, and can not find anyone having the same problem... Do someone got answers to this behavior?
Ok, I fixed it by moving the code to a new empty game object that control the timer and the score. And then it hit me that the issue was that I had that script running on 6 different components. So it was a lesson learned to structure the code :)
Then please do mark the question as solved, and save us some time to come and actually read your post in vain.
Answer by SunriseKingdom · May 01, 2014 at 11:47 AM
try putting this by where you want the code to stop
return;
Your answer
Follow this Question
Related Questions
Submitting bug reports 0 Answers
Timer Countdown problem 2 Answers
Unity 3.2 odd behavior (PROJECT USELESS) 1 Answer
If(Application.isPlaying == "MainMenu") then Show A messenger. take a info 1 Answer
Debug.log in Update without crashing 1 Answer