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 vnepomuceno · Oct 17, 2012 at 12:02 PM · gui.buttongui.box

GUI.Box doesn't show when GUI.Button is clicked

Hey! This is kind of a beginner's question, but I can't seem to make this work and I'm getting kind of frustrated. I have a tools box which contains two buttons, and I'd like that each time the player clicks one of those buttons, a GUI.Box will be drawn in the screen with some info.

This is the code I have:

 // C#
 private void OnGUI() {
 ...

 if (GUI.Button(addGroupRect, "+Group")) {
     Debug.Log("+Group");
     GUI.Box(new Rect(20, topPanHeight + 20, 100, 30), "Box1");
     }
 if (GUI.Button(addElemRect, "+Elem")) {
        Debug.Log("+Elem");
        GUI.Button(new Rect(40, topPanHeight + 20, 100, 30), "Elem");
 }



Any ideas on why nothing new is drawn when the buttons are clicked?

Thanks in advance,

Valter

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
1
Best Answer

Answer by Demigiant · Oct 17, 2012 at 01:31 PM

With your code, the GUI.Box is drawn only the instant (if...) the button is pressed. You should set a boolean variable that tells if the box needs to be drawn or not, and change that when a button is pressed.

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 vnepomuceno · Oct 17, 2012 at 04:22 PM 0
Share

I had tried before with the boolean variable, but it didn't work either.

Here's my code using the boolean:

 bool show = false;
         
 if (GUI.Button(addGroupRect, "+Group")) {
     show = true;
 }
 if (show) {
     GUI.Box(new Rect(20, topPanHeight + 20, 100, 30), "Box1");
     Debug.Log("+Box1");
 }

The more strange is that the string "+Box1" is printed in the console when the Button is clicked, but the Box is not drawn on the screen.

Since the number of boxes that will have to be displayed are not known before runtime, I'm not sure if using one boolean would work with multiple instances of Box and Button.

avatar image Demigiant · Oct 17, 2012 at 04:28 PM 1
Share

"bool show = false" should be declared outside of the OnGUI method. Otherwise you're resetting it to false each time OnGUI is called (many times x frame), and thus it will be still be true only the instant the button is pressed. If you need more than one box, than you'll need a List, but the logic stays the same.

avatar image vnepomuceno · Oct 17, 2012 at 08:28 PM 0
Share

Oh yes, of course, I forgot that. Thank you! Now it works with the bool variable code. Don't know why it doesn't with my previous one, though, but as long as it works, fine to me!

Thanks.

avatar image
0

Answer by m4s4m0r1 · Oct 17, 2012 at 01:29 PM

maybe you can adjust you GUI Depth. for more information, you can see in this site

http://docs.unity3d.com/Documentation/ScriptReference/GUI-depth.html

maybe it can help you...

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

11 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

Related Questions

How to make a GUI.Button open/close a GUI.Box? 1 Answer

GUI.Box inside of GUI.Button doesn't appear 2 Answers

show/hide GUI.box with a GUI.button 1 Answer

gui.box not resize based on screen resolution using c# code 0 Answers

Create a GUI on mouse click 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