My game has a pause menu script (attached to the main camera in every scene) that pauses/unpauses the game when you press Escape and opens the menu too. However, in the built version of the game, it still pauses/unpauses, but the menu doesn’t show! How might I fix that? Here’s the script (C#), in case it helps you:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public GUISkin guiSkin;
public float nativeVerticalResolution = 1200.0f;
private float nativeHorizontalResolution = 1900f;
private bool isPaused = false;
public string levelName;
private PlayerStatus status;
void Start() {
status = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerStatus>();
}
void Update() {
if (Input.GetKeyDown("escape")) {
print("Paused");
isPaused = !isPaused;
Screen.showCursor = isPaused;
Pause.TogglePaused ();
}
}
public void TogglePause() {
isPaused = !isPaused;
}
private bool isQuit = false;
void OnGUI (){
// Set up gui skin
GUI.skin = guiSkin;
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 ((float) Screen.width / nativeHorizontalResolution, (float) Screen.height / nativeVerticalResolution, 1f));
if(isPaused) {
if (!Screen.showCursor) Screen.showCursor = true;
// RenderSettings.fogDensity = 1;
GUILayout.BeginArea(new Rect (480, 200, 1000, 1000));{
GUILayout.Box ("Game Paused", "Title");
if (GUILayout.Button ("Reload Level")) {
Application.LoadLevel(levelName);
Time.timeScale = 1.0f;
isPaused = false;
Pause.TogglePaused ();
}
if (GUILayout.Button ( "Open Status Menu")) {
status.OpenStatGUI();
isPaused = false;
}
CameraControls.invertX = GUILayout.Toggle(CameraControls.invertX, "Invert Camera X Movement", "button");
CameraControls.invertY = GUILayout.Toggle(CameraControls.invertY, "Invert Camera Y Movement", "button");
if (GUILayout.Button ("Return to Game")) {
Time.timeScale = 1.0f;
isPaused = false;
Pause.TogglePaused ();
}
if (GUILayout.Button ("Quit Game")) {
//Time.timeScale = 1.0f;
isPaused = false;
isQuit = true;
Pause.TogglePaused ();
}
}GUILayout.EndArea();
} else if (isQuit) {
GUILayout.BeginArea(new Rect (480, 200, 1000, 1000));
GUILayout.Box ("Are you sure?", "Title");
if (GUILayout.Button ("Quit")) {
Time.timeScale = 1.0f;
isQuit = false;
Pause.TogglePaused ();
Application.LoadLevel("mainMenu");
}
if (GUILayout.Button ("Cancel")) {
isPaused = true;
isQuit = false;
}
GUILayout.EndArea();
}
}
}
Interestingly enough, there’s another script that pauses the game and opens a menu (the stats menu mentioned in this script) when you press “L”, and that menu works perfectly in the build.
EDIT: I’ve figured out that this OnGUI is not getting called at all. What could be doing that? I tried making it public, but that didn’t change anything either.
I got a Corrupted File Error on level1_data. Could that be it?
– CdraYes could be. Try making a new file and then copying the code into the new one.
– ExTheSeaLooks like the problem might have been a duplicate script name! There was a .js file with the same name (from another package). I also went ahead and tried what you said, and deleted the other script, either way it's working now! Thanks!
– CdraGood that it works now i will convert my comment to an answer so that you can accept it to mark the question as solved.
– ExTheSea