Question by
timewellspent · May 21, 2019 at 01:01 PM ·
timer countdown
Issues with countdown timer
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;
}
}
Comment
Best Answer
Answer by xxmariofer · May 21, 2019 at 01:30 PM
dont round timeñeft round the timeLeftText.text(remove the mathfround from updatetimer)
timeLeftText.text = Mathf.Round(timeLeft * 10.0f) / 10.0f;
Thanks! I don't know how I haven't tried this before.