Key/Button presses during a Pause menu (Time.timeScale) affecting the game

Hello!

I’m working on a Pause menu using (Time.timeScale) and I’ve come across this annoying thing that I can’t fix.

My game relies heavily on mouse button presses that activate different powers depending on the button. Each time it registers a button press, there’s a quick UI image fade-out to let the player know what power he’s using.
These powers last for as long as the player keeps said mouse button pressed, by the way.

Here’s the problem: I’ve noticed that keeping a button pressed while in the Pause menu and resuming the game (button still pressed) makes the UI image appear on screen, but it doesn’t fade out. It just stays there until I let go of the button, and then it’s instantly disabled.

Any idea on how to fix this? Thanks a lot!

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

public class Blink_Icon_1 : MonoBehaviour
{

    [SerializeField] private Image eyes_left;

    void Update()
    {
        if (Input.GetMouseButton(0))

            eyes_left.enabled = true;

        else
        {
            eyes_left.enabled = false;
        }

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

public class UI_Fade_Out : MonoBehaviour {

    CanvasRenderer rend;

    void Start () {

        rend = GetComponent<CanvasRenderer>();
       
    }

    IEnumerator FadeOut ()
    {
        for (float f = 1f; f > -0.45f; f -= 0.45f)
        {
            Color c = rend.GetColor();
            c.a = f;
            rend.SetColor(c);
            yield return new WaitForSeconds(0.05f);
        }
    }

    public void Update()
    {
        if (Input.GetMouseButtonDown(0))
            StartCoroutine("FadeOut");

    }
}

Sorry for the double post, but it just occurred to me: Could I fix this by fading the UI with animations instead of using float etc?

Your wait for seconds yield is based on the timescale. Which is set to zero. So it never finishes.
You need to wait based on unscaled time.
I forget if unity has a yield operator now for this.

If not you can make a custom one or change your yield to a while loop checking unscaled time and just yielding each frame.

Thanks! And with that while loop I’d be telling Unity to only check the button press when the game isn’t paused, right?

Not sure about that. But the wait for seconds yield is basically something like this.

start = Time.time
while(Time.time < start + delay)
{
yield null
}

You would replace the waitforseconds with a loop like this but using Time.unscaledTime instead of Time.time

There is WaitForSecondsRealtime for this exact purpose :wink: