Hello. I have a problem with my Pause menu script and GUI. I wrote a PauseGame script in C# and add this to my Pause Menu Canvas GameObject. When I type in this script in Start, that isEnabled is true and my GameObject.isActive(true), it is working but… When I start my game, the GUI is shown and working. When I hit Escape button, GUI is disabled but I cant now turn on back it with another Escape And when I type in this script in Start, that isEnabled is false and my GameObject.isActive(false), the GUI cant show when I hit Escape Can u help me with this? And I would like to add a Pause game when i hit Esc and unpause when i hit escape another time. Thanks for help if u will help me I am showing u a screens with this problem and script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseGame : MonoBehaviour {
public GameObject pauseMenu;
private bool isEnabled;
Update doesn’t run on objects that aren’t active. If you hit escape and then you disable the gameobject, your update no longer runs. You need to put the script on a empty gameobject that you never turn off in order for update to continue to run and respond to escapes even when your gui is off.
And also, please use code tags. It’s not fun to read unformatted code.
Thanks man! But I have next problem. When I put to this script Time.timeScale = 0, the game is paused but the GUI isnt show. Music is playing, game is paused, but GUI isnt shown… Maybe timescale isnt good for that? I have gameobject, not onGUI.
Sorry, I don’t understand. You want gui to show when you pause? You need to set it to active if you plan to show some gui. Music will continue to play, so you need to pause it on it’s own.
timeScale can work for pausing some things, but it depends on what you want paused as to if this will satisfy your needs. You may need to pause some things on their own or stop stuff from responding to input. Usually best done with a single static global variable that you can check against from any script.
I have that script but when I press Escape, music is playing, game is paused but Menu GUI isnt shew. I need to when I press Escape, game will pause, music will run, and GUI will show. What I have to do?
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseGame : MonoBehaviour {
public GameObject pauseMenu;
private bool isEnabled = false;
void Start()
{
pauseMenu.SetActive(false);
}
void Update()
{
// Enable pause menu
if ((Input.GetKeyDown(KeyCode.Escape)) && !isEnabled)
{
MenuOn ();
}
// disable pause menu
else if ((Input.GetKeyDown(KeyCode.Escape)) && isEnabled)
{
MenuOff ();
}
}
public void MenuOn()
{
pauseMenu.SetActive(true);
Cursor.visible = true;
isEnabled = true;
Time.timeScale = 0;
}
public void MenuOff()
{
pauseMenu.SetActive(false);
Cursor.visible = false;
isEnabled = false;
Time.timeScale = 1;
}
public void Resume()
{
MenuOff ();
}
public void MainMenu()
{
SceneManager.LoadScene (0);
}
public void Save()
{
PlayerPrefs.SetInt("currentscenesave", SceneManager.GetActiveScene().buildIndex);
}
public void Load()
{
SceneManager.LoadScene(PlayerPrefs.GetInt("currentscenesave"));
}
}