0
\$\begingroup\$

The pause menu is getting shown by default when I click the play button, even though the GameIsPaused boolean is set to false. I don't know if it's the fault of how I referenced it in the engine itself or how the script is written.

Here's how I referenced it in Unity:

Screenshot of canvas inspector

And here's my code:

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

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


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (!GameIsPaused)
            {
                Pause();
            }
            else
            {
                Resume();
            }
        }
    }
    

    public void Resume()
    {
        pauseMenuUI.SetActive(false);
        GameIsPaused = false;
        Time.timeScale = 1f;
    }
    void Pause()
    {
        pauseMenuUI.SetActive(true);
        GameIsPaused = true;
        Time.timeScale = 0f;
    }
    public void LoadMenu()
    {
        Debug.Log("Loading Game...");
        SceneManager.LoadScene("MainMenu");
    }
    public void QuitGame()
    {
        Debug.Log("Quitting Game...");
        Application.Quit();
    }
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Your code that toggles the pause menu active state runs only when the Esc key is pressed.

Did you mean to also run similar code in Start() to set the initial state of the menu when this script loads, something like...

void Start() {
    if (GameIsPaused)
    {
        Pause();
    }
    else
    {
        Resume();
    }
}

?

\$\endgroup\$
2
  • \$\begingroup\$ Yeah, I meant to set it to inactive by default \$\endgroup\$
    – Nineteenn
    Aug 4 at 12:18
  • \$\begingroup\$ You can also just untick the Pause menu object's "Active" checkbox in the top-left of the Inspector before running/building the game, if you prefer to do it without a start-up script. \$\endgroup\$
    – DMGregory
    Aug 4 at 12:19
0
\$\begingroup\$

I solved this problem by just disabling the PauseMenu just like this

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .