I’ve made a simple method of pausing my game within my GameManager class as seen here:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
private string[] levelList; //Create a list of levels
public int playerScore;
private bool isPaused;
void Awake() {
DontDestroyOnLoad(this.gameObject);
}
void Start () {
isPaused = false;
Screen.lockCursor = true;
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
pauseToggle();
}
}
void OnGUI() {
if (isPaused) {
GUI.Box(new Rect(-8, -8, Screen.width + 8, Screen.height + 8), "");
GUI.Box(new Rect(Screen.width / 2 - 160, Screen.height / 2 - 240, 320, 480), "Pause Menu");
if (GUI.Button(new Rect(Screen.width / 2 - 120, Screen.height / 2 - 200, 120, 80), "Close Menu")) {
isPaused = false;
Time.timeScale = 1.0f;
Screen.lockCursor = true;
Debug.Log("Unpaused");
isPaused = false;
}
if (GUI.Button(new Rect(20, 70, 80, 20), "Title Screen")) {
Application.LoadLevel(1);
}
}
}
void pauseToggle() {
if (!isPaused) {
Time.timeScale = 0.0f;
Screen.lockCursor = false;
isPaused = true;
}
else {
Time.timeScale = 1.0f;
Screen.lockCursor = true;
isPaused = false;
}
}
}
The problem is that the Escape key pauses/unpauses the game perfectly, however when the pause Resume button is clicked the GUI seems to freeze yet I can still toggle the pause state with Escape to get out of it.
I’ve tried both what is current under the Buttons if statement and simply using the pauseToggle function I made earlier. I must be missing something but I can’t seem to figure it out.