- Home /
Pause menu script not working in built game (but works fine in editor)?
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?
Yes could be. Try making a new file and then copying the code into the new one.
Looks 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!
Good that it works now i will convert my comment to an answer so that you can accept it to mark the question as solved.
Answer by ExTheSea · Apr 29, 2013 at 04:36 PM
I don't know if that's the case here but I also once had a problem where my script wasn't working in the build but worked in the the Editor. The problem was that the script got corrupted when i updated to Unity 4. What i did to solve my problem: Create a new Script and copy all the code from the corrupted one in the new one. Then just use the new Script. If you first want to see if that could be the problem make a build in development mode. Then make what normaly executes something inside your pause-script. If a Corrupted File Error appears that could be your problem.
Your answer
Follow this Question
Related Questions
Script using a Pause library causes audio to intermittently cutout. 0 Answers
Distribute terrain in zones 3 Answers
GUI not work after building 1 Answer
Free Gui solutions 2 Answers