Pause menu won't freeze time and player controller won't get disabled

I have just encountered a very weird bug in a game I am creating that I don’t know how I did it. I can’t pause the time when I hit pause and the player controller doesn’t get disabled. That persists even in the main menu (you can move the player in the scene).
No other script that interferes with Time.timeScale.
Thank you for the help much appreciated!

Hmm… it’s likely an error in your PauseMenuUI.cs script.
Paste it here, and we’ll have a look. Don’t forget to format it using the </> button.

using RPG.Control;
using RPG.SceneManagement;
using UnityEngine;

namespace RPG.UI
{
    public class PauseMenuUI : MonoBehaviour 
    {
        PlayerController playerController;
        private void Awake() 
        {
            PlayerController playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
        }

        private void OnEnable() 
        {
            if(playerController == null) return;
            Time.timeScale = 0;
            playerController.enabled = false;
        }

        private void OnDisable() 
        {
            if (playerController == null) return;
            Time.timeScale = 1;
            playerController.enabled = true;
        }
        public void Save()
        {
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
            savingWrapper.Save();
        }

        public void SaveAndQuit()
        {
            SavingWrapper savingWrapper = FindObjectOfType<SavingWrapper>();
            savingWrapper.Save();
            savingWrapper.LoadMenu();
        }

    }
}

The weird part is I tried to disable the player prefab from another script (in order to stop moving during a clip) and then I noticed the problem. I reverted the changes and scripts I put in for the video but the problem still persisted.

Let’s throw a couple of debugs in here to make sure things are working properly…

    PlayerController playerController;
    private void Awake() 
    {
        PlayerController playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
       if(playerController == null) Debug.LogWarning("Player could not be located");
    }

    private void OnEnable() 
    {
        if(playerController == null)
        {
            Debug.Log("OnEnable: No PlayerController");
            return;
        }
        Time.timeScale = 0;
        playerController.enabled = false;
    }

As soon as it starts in a scene apart from MainMenu console gives “OnEnable: No PlayerController” but you can still move. If you press ESC it gives you again the “OnEnable: No PlayerController” but you can still move and time doesn’t freeze. Also, I tried to disable the PlayerController and start. You can’t move but I don’t get the Debug.LogWarning. I am trying to search for some kind of Race Condition but Time.timeScale is not in other scripts.

Oops, 3rd look at this finally made the problem clear. In awake, you’re creating a local variable playerController that is overriding the global playerController reference.
Change Awake to simply playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
This will then reference the global playerController.

That was it! I never even noticed that! I thought that it was something like a Race Condition but I was careless. Thank you so much!!

It’s usually something I check for right away, because it’s happened to me as well.

If I were writing a programming language, the compiler would throw an error at declaring a local variable that hides a global name. (Actually, I believe Unreal’s flavor of C++ does exactly that).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms