Hi,
I made a very simple timer, and it stops at 0:0, but I want it to display “Game Over” when it hits that point. It stays at 0:0. How can I fix this?
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
I would not recommend using the operator “==” to compare floats, as they are not precise like integers.
Instead, your ‘else if’ statement should probably look something like this:
Never compare floats directly, but instead use Mathf.Approximately:
Also, in this particular case, when seconds = 0 the first branch might execute again if (seconds == 0), so we should doing something about that, just in case. Also, since you are using seconds -= Time.deltaTime, typically seconds are reduced below 0, and this is the main cause of the problem. So, here’s an Update() that works (I tested it):