- Home /
GUI resume button not working?
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.
There is no Resume button here, did you mean "Close $$anonymous$$enu" or do you have Resume somewhere else?
Yes sorry, running low on sleep. The close menu basically does what a resume button would.
I think this might be some kind of bug with the GUI on my end. Changed the code to match Drico's answer and still nothing. Weird thing is I just stuck a debug statement under the second button and it never got executed. It's almost as though the GUI doesn't function when the timeScale is 0. The most annoying part is that I've seen other people do this and it works but their code is always in JS.
Answer by VIPINSIRWANI · May 07, 2014 at 07:48 AM
Hi Once check your pauseToggle() is called on every Escape key press or print something inside pauseToggle() and one more thing you should mot use*Time.timeScale* instead of that you can use boolean values for pause or Resume Game.
Answer by DricoJD · May 07, 2014 at 09:58 AM
I Think I know the problem
Do not do (!isPaused)as this is a toggle option, which conflicts with you two sets of isPaused in ONGUI (). If you remove the double then test, if that does not work - which I highly predict it will not work then change !isPaused to (isPaused == true);
Answer by AzureKnight45 · May 08, 2014 at 12:14 AM
I fixed it guys, just set timeScale to some ridiculously low value like 0.0000001f. Basically paused as far as normal perception is concerned. Maybe it's a bug or not but the OnGUI function seems to mess up when timeScale is absolute zero.
Yes its a bit obscure but setting timeScale to 0 has be known to cause lots of problems when it comes to resu$$anonymous$$g! The problem is not limited to OnGUI either :P
Your approach is a good, common way to resolve this problem. Search on answers for Pausing Game and you'll see what I mean.
Answer by Wolfy1891 · Jun 20, 2021 at 12:09 AM
Obviously, this is a late answer. However, for anyone else looking for an answer.
I had this happen with my pause script. I fixed it by going into the unity input section and found that the button it had for cancel was also Esc.
I changed that to another button (specifically to backspace, but I'm sure any other button works) and there was no longer a fight for the ESC key.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't set Time.timeScale back to 1. 2 Answers
wait 3 seconds then resume c# 3 Answers
Unpausing While Keeping Values 0 Answers