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 AW0610AUT · Dec 27, 2013 at 01:19 PM · variablemenupauseopenclose

Pause Menu open/close with one key

Hello, I got a little problem here. I know why the code doesnt work, but i dont know how to get around this problem. I want to use the same button to open and to close my pause menu (escape). The problem is that when i set the first var (MenuOpen = true) my second if statement is true and it closes the menu again. How can i fix that.

The debug.logs where just there to test stuff out.

Here is the code:

 var OpenMenu : boolean = false;
 
 function Update() {
 
     if (Input.GetKeyDown ("escape") && OpenMenu == false){
         OpenMenu = true;
         Debug.Log("Menu Open");
         }
     if (Input.GetKeyDown ("escape") && OpenMenu == true) {
     OpenMenu = false;
     Screen.showCursor = false;
     Screen.lockCursor = true;
     Debug.Log("Menu Closed");
         }    
         
 }
 
 
 function OnGUI () {
     if (OpenMenu == true){
     GUI.Box (Rect (Screen.width/2-125,Screen.height/2-200,250,300), "Pause Menu");
     Screen.showCursor = true;
     Screen.lockCursor = false;
     Time.timeScale = 0;
     Debug.Log("I got here");
     
     
     
 }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sparkzbarca · Dec 27, 2013 at 01:27 PM

  if (Input.GetKeyDown ("escape")){
 
 if(!openmenu)

{ Debug.Log("Menu Open"); openmenu =true;

}
ELSE if(openmenu) //close menu;

} all you need is an else

Comment
Add comment · Show 3 · 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 AW0610AUT · Dec 27, 2013 at 01:51 PM 0
Share

Ahh ok thanks to both of you. I knew there was a easy solution. I still have a problem. $$anonymous$$y mouse also moves my caracter ($$anonymous$$ouseLookScript) can i freeze that with a command too?

avatar image ArkaneX · Dec 27, 2013 at 02:19 PM 0
Share

@AW0610AUT - it would be better to post this as a new question. But of course it's possible to disable $$anonymous$$ouseLookScript after pressing a key, and enable it on the second press of this key:

 var script : $$anonymous$$ouseLookScript = GetComponent($$anonymous$$ouseLookScript);
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
 {
     script.enabled = !script.enabled;
 }
avatar image ArkaneX · Dec 27, 2013 at 02:28 PM 0
Share

@sparkzbarca - I understand correcting an answer, when it contains a mistake. But seeing edit which totally changes the incorrect answer, copying details from another one which is correct, is just disappointing. If you see that your answer is incorrect, please delete it.

avatar image
1

Answer by ArkaneX · Dec 27, 2013 at 01:29 PM

You can use if ... else:

 if (Input.GetKeyDown(KeyCode.Escape))
 {
     if (!OpenMenu)
     {
         Debug.Log("Menu Open");
     }
     else
     {
         Screen.showCursor = false;
         Screen.lockCursor = true;
         Debug.Log("Menu Closed");
     }
     OpenMenu = !OpenMenu;
 }

Comment
Add comment · 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
0

Answer by Frank126 · Dec 27, 2013 at 02:14 PM

 var OpenMenu : boolean = false;

 function Update() {
     if (Input.GetKeyDown ("escape")){
         if (OpenMenu){
             OpenMenu = false;
             Screen.showCursor = false;
             Screen.lockCursor = true;
             Debug.Log("Menu Closed");
         }else{
             OpenMenu = true;
             Screen.showCursor = true;
             Screen.lockCursor = false;
             Time.timeScale = 0;
             Debug.Log("Menu Opened");
         }
     }
 }
 function OnGUI () {
     if (OpenMenu == true){
         GUI.Box (Rect (Screen.width/2-125,Screen.height/2-200,250,300), "Pause Menu");
         Debug.Log("I got here");
     }
 }

Note that you should never use the OnGUI() method to do logic since it is a drawing method that can be called more frequently then the Update() method (Source).

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 AW0610AUT · Dec 27, 2013 at 02:22 PM 0
Share

Ok thanks. I´ll cange my script a bit then. Thanks for the info.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script Not workin 1 Answer

Pause Menu Audio 1 Answer

Halt function midway until player presses 'OK' then continue 1 Answer

Problems with pause menu displaying items and stopping player movement 2 Answers

Opening, Closing, Keeping door open at proximity 1 Answer


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