How Can I Pause The Game But Not The Canvas?

Okay so I have 1 main issue by the looks of it, my UI/PauseMenu gets paused so none of my animations in the UI get played (They just freeze on the side of the screen instead of moving to the middle because the animation is paused/Time.Delta = 0f)
How can I pause the game but not the Canvas?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PauseMenu : MonoBehaviour
{
    public static bool GameIsPaused = false;
    public GameObject pauseMenuUI;

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameIsPaused)
            {
                Resume();
            } else
            {
                Pause();
            }
        }
    }


    public void ReturnToMenu()
    {
        SceneManager.LoadScene("Menu");
    }


    void Resume ()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
    }
    void Pause ()
    {
        pauseMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameIsPaused = true;
    }
}

You have to use something that is called “UnscaledTime” for the elements you don’t want to pause.
In your example you have an animator you don’t want to pause. Grub that animator in the inspector. Then, under “UpdateMode” choose “UnscaledTime”.
That animator will now keep working regardless of the TimeScale. You can also change the update mode by script if you need to.
Another thing you need to change for the things you don’t want to pause are any functions and coroutines using Time.time. Go and change “Time.time” for “Time.UnscaledTime”.
If you need to replace “Time.deltaTime” you use “Time.UnscaledDeltaTime” instead.

2 Likes

Thanks it worked, any idea how to fix the 1st menu button being highlighted automatically when i am not hovering over it? I tried a fix already but it didn’t work. I changed the Navigation to None, under the Button (Script) but i have still have the issue.

4693007--442718--upload_2019-6-28_1-41-48.png

Unity controls can be “selected” by navigating with the keyboard, but even without navigation they still get selected when you click them. Have you considered changing the “selected” transition for the button to something less noticeable?

(The fact that there’s a “selected” trigger in that list at all is a recent change that I hadn’t noticed until now. It’s not even mentioned in Unity’s documentation. But I’m glad they finally added it!)

1 Like

Thanks, I could set them all to Normal apart from Selected as a quick fix also, i’ll just do this for now.

2 Issues down, 1 to go xD. Now I just have to figure out how to make my Pause work more than once, after it gets closed it doesn’t pause again. Probably an easy fix online somewhere. I assumed FixedUpdate instead of Update woulda fixed it, but nope.
My theory is that my controls are breaking the pause the menu. I guess i have it coded to Open the menu, but not to close i guess. Maybe i just need to finish doing the Resume button then it will open n close correctly, apart from the issue with my rocketlauncher activating on pause menu if i press Spacebar, which then resumes my game and breaks it as well.

The GUI is very difficult because of this, it can make you loose a lot of time and be frustraiting. It can really take you as much as other more interesting problems. There are countless of contradictions that can be generated by opening a menu from a button a spacebar and by script. This doesn’t mean you shouldn’t do it, but it takes patience.
In your particular case, there are a lot of things that may be going on, like for example you may be using a toggle instead of a button,and since you haven’t clicked, the isActive may be activated when you click but not when you open the menu from other side. There’s also the question of why you set gameIsPause to true but you never set it to false. We can’t see what you use this var for, so that may also be the cause. Ultimately you’re gonna have to test it more and try to pin down when is it that it breaks. What exactly breakes it.
As a separated note, sometimes you may have variables that are interconected, things that should changed you change together. Is very easy to generate contradictions this way. In those cases, you may want to use a setter property to make sure that when a variable is changed the interconected values are also changed. This can solve many of this contradictions.

Edit: Just realized that Spacebar and Enter are both used to click on the Selected button, i didn’t code that so i didn’t expect it lol, at least i figured that out, guess i’ll just fix the UnscaledTime on 2nd pause issue.
“gameIsPause to true” That part was a mistake, i managed to fix several other things, also about the spacebar i basically meant that my player attack controls are removing the pause menu/effect for some reason, so i need to google how to lock/disable player/vehicle controls whilst it’s paused and on the menu.
Also another issue appeared after i fixed most of them xD I can get the menu to appear perfectly and go away and come back now, but the player attack controls will resume it which i don’t want, and the new issue is whenever the game is paused for a 2nd time, it doesn’t apply UnscaledTime, only on the 1st time. So the animation just freezes.

Just finished work for the week also, there was no hoover bags so i had to use a hand brush and dustpan to sweep like 20 offices, drippin with sweat but i have plenty of time to work on this now lol.