Timer Not Responding to Pause

I am trying to create a timer which pauses when it gets down to a certain number.
This code seems like it should work to me, but the timer does not pause at 1075seconds, nor 1035 seconds. The timer is supposed to resume after 3 second, then.
Any idea what is wrong, and how to get this working?
I have sampled my issue down to two methods. The PlayAndPauseButton method works fine when initiated with a UIElement Button.
Thanks so much!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class TestPausePlay : MonoBehaviour
{
    float Duration = 1080;
    bool TimerIsRunning = true;
    [SerializeField] private TMP_Text TimerText;

void Update(){
    if (TimerIsRunning == true) {
    Duration -= Time.deltaTime;
    TimerText.text = Duration.ToString();
        if (Duration == 1075){
            TimerIsRunning = false; // Does not happen
        } else if (Duration == 1035){
            TimerIsRunning = false; // Does not happen
        }
    }
}

public void PlayAndPauseButton(){
        if (TimerIsRunning == false){
            TimerIsRunning = true;
            //PauseButtonLabel.text = "Pause";
        } else {
            TimerIsRunning = false;
            //PauseButtonLabel.text = "Play";
        }
        }
}

Note that in Unity you should add an ‘f’ to the end of float values

float duration = 1080f;

or

float duration = 1080.0f;

The latter might help you see why your comparison test is likely to fail.

The value returned by Time.deltaTime is not an integer, it’s a float. It will be a slightly different value across frames. Added to this, floats have a certain amount of imprecision.

It’s extremely unlikely that your Duration variable would ever equal EXACTLY 1075 or 1035.

So you can’t compare your timer value against such a precise number. There’s an example of how to use the update loop to implement a timer in the documentation:

1 Like

Thanks, I have done that, but the timer still won’t stop with the ‘==’.
I’m thinking about just giving a range, ie. Duration > 1074.9f && Duration < 1075.1f
As quirky as I have found trying to do it with ‘==’, using a range actually works every time and is a simple solution.

As mentioned, a floating point value will rarely ever, usually never, exactly equal another value. You should generally never compare them with ==. You usually need to work with a approximate comparison, or just less than or greater than your desired threshold.

1 Like

From my side, I suggest creating an array for pauses, which will make it more convenient and reduce the amount of code. In the Update function, first predict the future time, then iterate through all pauses to find the one that is greater than the current time and at the same time less than the future time (that is, it falls within the range of time that will pass during this frame).

public float Duration = 1080f;
public float[] Pauses = { 1035, 1075 };


void Update()
{
    float nextDuration = Duration - Time.deltaTime;

    foreach (float pauseTime in Pauses)
    {
        if (Duration > pauseTime && nextDuration <= pauseTime)
        {
            print($"Pause for: {pauseTime} duration ");
        }
    }

    Duration = nextDuration;
}