Once my time hits 0 it will continue to display negative counting, How to make it STOP at 0?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class Count_Down : MonoBehaviour
{
public float totalTime;

public Text text;

private float minutes;
private float seconds;

private void Update()
{
    totalTime -= Time.deltaTime;

    minutes = (int)(totalTime / 60);
    seconds = (int)(totalTime % 60);

    text.text = minutes.ToString() + " : " + seconds.ToString();

}

}

2 Answers

2

You should only decrease totalTime if it is greater or equal than 0 by surrounding the code that is related to the decreasing of the timer in an if statement.
In addition, since totalTime is a float that can become negative in one decrement (without reaching 0), you should also check if it is less than 0, and set it to 0 if that is true.

I think this will work, because when working with health or something similar you do this.

So basically you say

if(time <= 0)
{
time = 0;
}

This makes it so that when ever time is less than zero, it will just become 0.

Note that there is no need to trigger the decrement code every tick if the time has already reached 0, so in addition to what you mentioned, the code that decrements the timer should only be triggered when timer is greater than 0.