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 · Apr 25, 2010 at 12:48 AM · guibuttongui-button

Turn off my GUI Box when I click a button.

I'm trying to get a script going that will perform different functions based off of the buttons you press. The eventual end result will be; when you click on a door to go to the next scene, it will pop up a box confirming that you'd like to go. If you click yes, It will Load the next level, if you click no, it will simply turn the GUI back off until you click the door again.

I'm trying it out on a box (it should yield the same results in the end game) If you click "yes", it destroys the box... that part works... however, I can't figure out what to do to make "no" turn the GUI off.

function OnGUI() { GUI.Box(Rect(100,100,300,100),"Would You Like To Destroy This Box?");

 if(GUI.Button(Rect (100,150,100,25),"Yes"))
 {
     Destroy (gameObject);
 }
 if(GUI.Button(Rect(300,150,100,25),"No"))
 {
     //What Goes Here?
 }

}

Any Ideas? Thanks much!

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 Eric5h5 · Apr 25, 2010 at 01:00 AM

Put "enabled = false;" there.

To re-enable, use

function OnMouseUp () {
    enabled = true;
}

This way you don't need extra logic, and the OnGUI function only runs when needed, not all the time.

Comment
Add comment · Show 7 · 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 · Apr 25, 2010 at 01:07 AM 0
Share

That solved that problem, Thanks. Now I just need to figure out how to activate the GUI when I click on the object. It was working before, but now, not so much...

Thanks! =)

avatar image Cyclops · Apr 25, 2010 at 01:31 AM 0
Share

@CalledToGa$$anonymous$$g, once you disable the GameObject (with this answer), it won't respond to mouseclicks any more, which is why it's not working.

avatar image Eric5h5 · Apr 25, 2010 at 01:51 AM 0
Share

@CalledToGa$$anonymous$$g: See my edit...just set enabled = true.

avatar image Eric5h5 · Apr 25, 2010 at 01:56 AM 0
Share

@Cyclops: That's incorrect...On$$anonymous$$ouseDown, etc., will still respond when a script is disabled. Setting enabled = false doesn't disable the entire game object (you need gameObject.active = false for that), it only disables the script.

avatar image Cyclops · Apr 25, 2010 at 01:59 AM 0
Share

@Eric5h5, really? Hm, I wasn't clear what the enabled = false was referring to, I figured it was the GameObject, not the attached script. So - when the script is disabled, it still responds to mouse clicks? Seems odd to me - not sure what enabled does then. But, I stand corrected...

Show more comments
avatar image
1
Best Answer

Answer by Cyclops · Apr 25, 2010 at 01:23 AM

I think what you want is, to not display anything until a box/door is clicked (rather than disabling the GameObject entirely). Consider re-writing it like this:

var showChoice : Boolean;

function Start() { showChoice = false; } function OnGUI() { if (showChoice == false) return; GUI.Box(Rect(100,100,300,100),"Would You Like To Destroy This Box?"); if(GUI.Button(Rect (100,150,100,25),"Yes")) { Destroy (gameObject); } if(GUI.Button(Rect(300,150,100,25),"No")) { showChoice = false; } } function OnMouseUp() { showChoice = true; }

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 · Apr 25, 2010 at 01:30 AM 0
Share

Yeah, Basically, I'm trying to be able to click on the object (door or in this sample case, a box) and make the GUI pop up when I click the object. When I click "No" I want the GUI to disappear, but I want to be able to recall that GUI if i click on the object again.

In the case that it's to load a level, if I decide to wait, I want the GUI to be able to pop back up again when I come back to the door.

avatar image Cyclops · Apr 25, 2010 at 01:33 AM 0
Share

@CalledToGa$$anonymous$$g, this should do it - assu$$anonymous$$g you got the last version :) I just changed it to have On$$anonymous$$ouseUp().

avatar image CalledToGaming · Apr 25, 2010 at 01:36 AM 0
Share

Eureka! You're a godsend. Thanks much! It works Exactly as I hoped it would.

avatar image
0

Answer by SARWAN · Nov 05, 2012 at 01:13 PM

Create a gameobject and insert your OnGUI() inside of it after some GUI buttons clicked just destroy the game object.

 void OnGUI()
     {
         GUI.Box(new Rect(600,300,300,270), "Loader Menu");
         if(GUI.Button(new Rect(650,350,70,30), "EASY"))
         {
             Destroy (this.gameObject);
         }
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

How to create a gui button inside an if statement? 1 Answer

Cases if a GUI.Button is clicked two or more times?? 2 Answers

Making a Hover function for a button or toolbar from code. 2 Answers

How to change a GUI button texture through scripting 1 Answer

button pressed twice / Conversation text skip 5 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