- Home /
Counting Down Timer But My Variable is not reaching Zero ever, Why?
ok this is my code and it never seems to reach zero. I don't understand why my Variable myTimer doesn't reach zero ever. I even put a debug messege thing to tell me if myTimer more than zero and also when my timer is zero. but the one to tell me when my timer is zero never appeared cause like i said it doesn't seem like myTimer is reaching zero. The following code is in my Update() method
As soon as user clicks key G, set myTimer to 4.0f (forseconds)
//By the way myTimer is a float
if( myTImer > 0 )
myTimer -= Time.deltaTime;
//do something
}
else if( myTimer == 0){
//do something until count down something
//when count that something
//then set myTimer = -1
}
line 5 has a spelling mistake and is missing an opening parenthesis. Always check your spelling and opening/closing brackets/parenthesis.
if( myTImer > 0 ) // capitol I, no opening curly braces
should be
if( myTimer > 0 ) {
Answer by DannyB · Sep 09, 2013 at 03:15 PM
Use else if( myTimer <= 0 )
(or just else
) instead of else if( myTimer == 0 )
There is very little chance that reducing Time.deltaTime
from any number would reach 0 exactly.
Also, when dealing with floats, you should consider using Mathf.Approximately for float comparison. It will not help you (and not necessary) in this case, but you may need it for other cases.
Finally, for a proper debug in the future, you should have placed a Debug.Log( myTimer )
outside of both conditions, and you could have seen the values going through zero.
I would just change "else if ( myTimer == 0 )" to a simple "else".