Is there a way to make WaitForSeconds ignore time.timescale?

Is there a way to make WaitForSeconds ignore time.timescale, or in the situation below, is there a replacement for WaitForSeconds I can use instead?

I’m using:

    while (!clicked)
    {
        yield return new WaitForSeconds(0.01f);
    }

to wait for “clicked” to be true before continuing execution of a coroutine.

I would like to be able to pause the game, but discovered that time.timescale = 0 prevents WaitForSeconds from completing so the while loop can exit.

I’m aware that I may just have to disable GameObjects/scripts/animations etc manually in order to pause them, but I wondered if there was a better way.

Thank you for reading. Any help is appreciated.

No, WaitForSeconds always uses Time.timeScale. However in your case you can simply use

yield return null;

You try to wait for 0.01f seconds. Usually your framerate is lower than 100fps. So the time between two frames is already longer than the time you want to wait.

If you actually need something like WaitForSecond, you can use a coroutine like this:

public IEnumerator _WaitForRealSeconds(float aTime)
{
    while(aTime >0f)
    {
        aTime -= Mathf.Clamp(Time.unscaledDeltaTime,0, 0.2f);
    }
}
public Coroutine WaitForRealSeconds(float aTime)
{
    return StartCoroutine(_WaitForRealSeconds(aTime));
}

Those can be used like this in another coroutine:

while(true)
{
    yield return WaitForRealSeconds(2f);
    // every 2 seconds, regardless of timescale.
}

You might want to have a look at my CoroutineHelper class. It uses a singleton to run helper coroutines indepentently from a GameObject. At the moment it doesn’t contain a wait function that uses unscaledTime / unscaledDeltaTime but it could be easily added.