To put simply: I’m having issues with my countdown timer.
I’m probably overlooking something. The timer is supposed to count down to 0.0 from 30.0, but when running the code, the timer stops at 29.8 or 29.7. I realized that removing the Mathf.Round() code fixes the problem, and the timer counts down to zero. I don’t know why this is happening.
How do I round off the timer while also getting it to count down to zero?
public Text timeLeftText;
public float timeLeft;
public bool gameOver;
void Start() {
timeLeft = 30.0f;
gameOver = false;
}
void Update() {
UpdateTimer();
timeLeftText.text = string.Format("{0}", timeLeft);
}
void UpdateTimer() {
if (timeLeft >= 0.0f) {
timeLeft -= Time.deltaTime;
timeLeft = Mathf.Round(timeLeft * 10.0f) / 10.0f;
}
else {
gameOver = true;
}
}