Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CalledToGaming · Mar 04, 2010 at 08:48 AM · javascriptguibuttonquit

Use a key to open a GUI?

I'm trying to implement a feature that would allow me to press a key and bring up a GUI. For example, I press Esc, and it brings up a GUI button that allows me to quit the game.

The problem I'm having is that When I press the button, they GUI stays up until the button is released. I want it to stay up until I press the button again. Obviously I'm missing a piece of code, here's what I have so far (JS)

function OnGUI () { if(Input.GetButtonDown("Esc")){ if(GUI.Button (Rect (10,10,100,20), "Quit")) { Application.Quit(); } }

}

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

2 Replies

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

Answer by Ashkan_gc · Mar 04, 2010 at 08:55 AM

you should have a variable called quitMenu weh nyou press esc set it to true and then when you want to hide the menu set it to false.

if (GUI.GetButtonDown ("ESC")) { quitMenu=true;}
if (quitMenu == true)
{
if (GUI.Button (Rect(10,10,300,300),"quit")
{
//place your code here and also put
quitMenu = false;
}
}

define the quitMenu on top of your script as a private var.

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 CalledToGaming · Mar 04, 2010 at 07:13 PM 0
Share

Well, that helps get the key to pop and stay up, Now I just need a script to get the key to disappear if pres "Esc" again. here's what I have so far...

private var quit$$anonymous$$enu;

function OnGUI () { if (Input.GetButtonDown ("Esc")) { quit$$anonymous$$enu=true;} if (quit$$anonymous$$enu == true) { if (GUI.Button (Rect(10,10,50,50),"Quit")) { Application.LoadLevel("Opening Screen");

}

}

}

avatar image Ashkan_gc · Mar 04, 2010 at 08:10 PM 0
Share

you should just have something like this inside your if. if (Input.GetButtonDown ("Esc")){quit$$anonymous$$enu=!(quit$$anonymous$$enu);} so if quit$$anonymous$$enu is true it will make it not(true) and if it's false it will make it not(false). in your GUI.Button code just make quit$$anonymous$$enu false if your code is something other than changing the scene.

avatar image CalledToGaming · Mar 05, 2010 at 06:39 PM 0
Share

I can't seem to get it to work. Either I'm not placing it right, or I just don't understand what I'm doing. (Both of which are very likely scenarios)

avatar image
0

Answer by Coreyf716 · Sep 29, 2012 at 04:49 PM

Try this:

 var quitMenu : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
 
 if(Input.GetKeyDown(KeyCode.Escape)) {
 
 quitMenu = !quitMenu;
 
 }
 
 }
 
 function OnGUI () {
 
 if(quitMenu == true) {
 
 if(GUI.Button(Rect(10,10,300,300), "quit")) {
 
 Application.Quit();
 
 }
 
 }
 
 }
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

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

1 Person is following this question.

avatar image

Related Questions

Key for GUI.Button 2 Answers

Best way to create large amount of buttons 2 Answers

How do I make the buttons work on this GUI? 2 Answers

Assign a hotkey to a GUI button? 3 Answers

GUI Info button not displaying 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