- Home /
When I pause my game and enable canvas and then resume my keyboard starts controlling the menu...
I'm struggling with a very strange problem in Unity 4.6...
I have a canvas which is my pause menu. When escape is pressed I show it to the user, he has the option to resume, view help or return to the main menu. The functions connected to these buttons are all in one script called PauseMenuActions. They change variables in my MenuController script to resume/ show helpcanvas/ other stuff...
This is how I can invoke this very weird bug:
I start the level, press escape to show menu and press escape again to disable it. I can now continue playing the level without problems. This DOES NOT invoke the problem.
When I press escape again and press the resume button (which calls the function in PauseMenuActions which changes showMenu to false in MenuController which causes resume in OnGUI()) the game resumes, but when I then am walking forwards (Z because I'm on azerty - ZQSD) and space to jump, the pause menu pops up out of nowhere. It's not with every key + space bar, I can invoke it with Z + space, D+space, and when I do S+space the return to main menu gets called...
So what seems to be the problem: from the moment when one function of the PauseMenuActions gets called, some key combinations start invoking other methods from that very script, without me registering input and invoking the methods myself.
Does anyone have any clue or idea what might be the problem? It's extremely frustrating, any help will be immensely appreciated!
Code in menuController:
using UnityEngine;
using System.Collections;
public class MenuController : MonoBehaviour {
public static bool showMenu = false;
public static bool showHelp = false;
public Canvas pauseCanvas;
public Canvas helpCanvas;
// Use this for initialization
void Start () {
pauseCanvas.enabled = false;
helpCanvas.enabled = false;
showMenu = false;
Time.timeScale = 1.0f;
(GameObject.Find("firstPersonController").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
(GameObject.Find("cameraMain").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
Screen.lockCursor = true;
Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Escape)) {
showMenu = !showMenu;
Debug.Log("Escape pressed showmenu: " + showMenu);
}
}
void OnGUI() {
if(showMenu)
{
if(showHelp) {
pauseCanvas.enabled = false;
helpCanvas.enabled = true;
} else {
pauseCanvas.enabled = true;
helpCanvas.enabled = false;
}
Time.timeScale = 0.0f;
(GameObject.Find("firstPersonController").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
(GameObject.Find("cameraMain").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
Screen.lockCursor = false;
Screen.showCursor = true;
}
else
{
showHelp = false;
pauseCanvas.enabled = false;
helpCanvas.enabled = false;
Time.timeScale = 1.0f;
(GameObject.Find("firstPersonController").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
(GameObject.Find("cameraMain").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
Screen.lockCursor = true;
Screen.showCursor = false;
}
}
}
Code in PauseMenuActions:
using UnityEngine;
using System.Collections;
public class PauseMenuActions : MonoBehaviour {
public Canvas pauseCanvas;
public Canvas helpCanvas;
public void Resume()
{
Debug.Log ("resume");
MenuController.showMenu = false;
MenuController.showHelp = false;
}
public void showHelp()
{
MenuController.showMenu = true;
MenuController.showHelp = true;
Debug.Log ("show help");
}
public void BackToMenu()
{
MenuController.showMenu = true;
MenuController.showHelp = false;
Debug.Log ("back to menu");
}
public void BackToMain()
{
MenuController.showMenu = false;
MenuController.showHelp = false;
Time.timeScale = 1.0f;
(GameObject.Find("firstPersonController").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
(GameObject.Find("cameraMain").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
Screen.lockCursor = false;
Screen.showCursor = true;
Debug.Log ("Back to main");
Application.LoadLevel ("MainMenu");
}
}
How my menu looks: http://i.imgur.com/UJkv6rZ.png
Answer by AcE_fLoOdEr · Jan 01, 2015 at 07:25 AM
Do you want a better solution and save yourself some scripting? I mean... I used OnGUI() when 4.6 wasn't out yet, with the new UI it's much easier to script a pause menu etc.
1). Create a canvas
2). Set up the pause menu layout (use buttons etc etc)
3). rename that canvas to "Pause Menu"
4). Make a new script call it "PauseGame" copy the following to that script
using UnityEngine;
using System.Collections;
public class PauseGame : MonoBehaviour {
public GameObject pauseMenu;
private bool isEnabled = false;
void Update()
{
// Enable pause menu
if (Input.GetKeyDown(KeyCode.Escape) && !isEnabled)
{
pauseMenu.SetActive(true);
isEnabled = true;
}
// disable pause menu
else if (Input.GetKeyDown(KeyCode.Escape) && isEnabled)
{
pauseMenu.SetActive(false);
isEnabled = false;
}
}
}
5). After that, drag and drop the "Pause Menu" game object to the script's GameObject field that we created in the script
6). Disable the "Pause Menu" game object in editor by unchecking the checkbox next to its name in the inspector
7). start the game and try it out. cheers
Just a quick message saying this really helped me out! :)
I dont know policies on unity answers if writing a comment to something done in 2014 is bad form or not... but there was nothing marked as "good answer" and THIS is a good answer!! :)
I did not use your code itself, but the logic to enable or disable the menu itself from a separate canvas worked perfectly to make sure you cant move or use the menu while the canvas is disabled :)
so, THAN$$anonymous$$S! :)