0
\$\begingroup\$

the code when it works right should pause the game (which means setting the timescale to 0)when you press esc and bring up a pause menu. it does this, but when i press the menu button( which brings me to the main menu) and then press play(brings me to level 1) the level starts off with a timescale of 0 and i have to press esc for the game to start.

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 Start()
    {
        pauseMenuUI.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (GameIsPaused)
            {
                Resume();
            }
            else
            {
                Pause();
            }
        }
    }
    public void Resume()
    {
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        GameIsPaused = false;
    }

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

    public void LoadMenu()
    {
        SceneManager.LoadScene("main menu");
    }
}
```
\$\endgroup\$
4
  • \$\begingroup\$ Where do you set time scale back to 1 when returning to the main menu? \$\endgroup\$ Commented May 30, 2021 at 21:23
  • \$\begingroup\$ oh I didn't know I needed to I thought it was just for one scene(I'm new to coding and unity) but I guess it was frozen in the main menu too I just couldn't tell. what sort of code would that be? \$\endgroup\$ Commented May 30, 2021 at 22:31
  • \$\begingroup\$ Want to post your solution as an Answer below? \$\endgroup\$ Commented May 30, 2021 at 23:52
  • \$\begingroup\$ yeah sorry ill do that from now on \$\endgroup\$ Commented May 31, 2021 at 1:49

1 Answer 1

1
\$\begingroup\$

just put the timescale set to 1 under the public void load menu.

 public void LoadMenu()
{
    SceneManager.LoadScene("main menu");
    Time.timeScale = 1f;
}
\$\endgroup\$
1
  • \$\begingroup\$ Remember to mark this answer as Accepted if it worked for you. \$\endgroup\$ Commented Jun 30, 2021 at 11:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.