- Home /
Simple timer / if statement question
I have this code running a timer that ticks down with the deltaTime
var myTimer : float = 2.0;
function Update() {
Debug.Log(myTimer);
myTimer -= Time.deltaTime;
if (myTimer <= 0) {
Debug.Log ("myTimer < 0");
myTimer = 2.0;
}
}
So this prints to the console myTimer decrementing toward zero and beyond. When myTimer becomes less than zero for the first time then the if statement is true "myTimer < 0" fires as it should and myTimer is reset to 2.0. From here on out, however, when the if statement is true and the myTimer is reset to 2.0 the Debug.Log("myTimer < 0"); never fires. I am perplexed as to why the timer updates but the Debug.Log command never fires.
Any thoughts?
Cheers
I dunno. That works perfect for me when I copied and pasted it into an empty javascript file.
You don't normally need to do that sort of thing manually anyway; use InvokeRepeating or yield WaitForSeconds most of the time.
Your answer
Follow this Question
Related Questions
Fuel Script error 2 Answers
Sending Variable to Highscore Server results in 0. 0 Answers
Unity ignoring if statement for high score 2 Answers
End Game when hit's water 2 Answers
If Event Keeps Occurring frequently then Do Something 0 Answers