Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eliasvdb · Dec 31, 2014 at 07:09 PM · c#guimenuevent4.6

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

alt text alt text


active.png (245.2 kB)
inactive.png (233.0 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FireHawkX · Apr 27, 2016 at 08:49 PM 0
Share

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! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Example of setting up a button OnClick event via scripting? 2 Answers

ArgumentException: method return type is incompatible 2 Answers

Recreate Unity Editor Window in Game 0 Answers

[4.6] Add scroll wheel functionality to scroll rect 2 Answers

Unity v4.6 Joystick? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges