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 murkantilism · Aug 04, 2012 at 08:29 PM · guiifgui.buttonnested

Unity GUI Nested If statements causing GUI Buttons not to render

I have a Map Interface that has a series of buttons that when clicked will load a new scene, effectively a "Fast Travel" mechanic. Currently there is only 1 button available to click, whihc returns the player to their Home Village. Initially I just had it load the Home Village scene, and everything worked perfectly. Now I'm trying to add a pop-up so when the player tries to return home, it will first create a label asking "Are you sure you want to return home?" and create a "Yes" and "No" button. When I tried to add nested if statements to achieve this howevoer, the label and 2 buttns are not rendered for some reason. Is there a known problem with nested GUI-related If statements? My code of OnGUI() is below.

function OnGUI(){ // If the map has been opened, create all of the location buttons if(mapOpenp == true){ mapInterface.enabled = true;

     // If the Home Village button is clicked...
     if( GUI.Button((new Rect (500, 400, 50, 50)), homeVillageBtnTxtr, homeVillageStyle) ){
                     
         Debug.Log("Home Village Button Clicked");
         
         //Application.LoadLevel("BuildingInterface");
         // The above line, when uncommented, loads the BuildingInterface scene when the
         // button is clicked, testing that the button itself works correctly.

         // ... create a label asking if they want to return home,
         GUI.Label((new Rect (100, 200, 75, 75)), "Are you sure you want to return home?");
                     
         // ...and create a Yes and No button. If Yes is clicked, load the Building Scene.
         // If no is clicked, close the Map interface.
         if(    GUI.Button((new Rect (100, 250, 50, 50)), "Yes") ){
             Debug.Log("Yes button has been created + clicked");
             Application.LoadLevel("BuildingInterface");
         }
         
         if ( GUI.Button((new Rect (150, 250, 50, 50)), "No") ){
             Debug.Log("No button has been created + clicked");
             GUI.enabled = false;
         }            
     }
 }else{
     // Otherwise the map has been closed, so disable the buttons
     GUI.enabled = false;
 }

}

Solution (thanks to Eric5h5) using a boolean variable rather than nested If statements:

function OnGUI(){ // If the map has been opened, create all of the location buttons if(mapOpenp == true){ mapInterface.enabled = true;

     // If the Home Village button is clicked...
     if( GUI.Button((new Rect (500, 400, 50, 50)), homeVillageBtnTxtr, homeVillageStyle) ){
         // ... set the "has the Village button been clicked" boolean to true.
         homeVillageBtnp = true;    
     }
     
     // If the Village button has been clicked...
     if(homeVillageBtnp){
         // ...create a label asking if they want to return home...
         GUI.Label((new Rect (100, 200, 75, 75)), "Are you sure you want to return home?");
         
         // ...and check if the Yes button has been clicked...
         if( GUI.Button((new Rect (100, 250, 50, 50)), "Yes") ){
             // If the yes button is clicked, load the Building Interface
             Application.LoadLevel("BuildingInterface");
         }
         
         // ...and check if the No button has been clicked.
         if ( GUI.Button((new Rect (150, 250, 50, 50)), "No") ){
             // If the no button is clicked, close the "pop-up"
             homeVillageBtnp = false;
         }
     }            
 }else{
     // Otherwise the map has been closed, so disable the buttons
     GUI.enabled = false;
 }

}

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

Answer by Eric5h5 · Aug 04, 2012 at 08:32 PM

GUI buttons only return true for the one frame in which they are clicked. So everything inside the "if (button)" statements will only render for part of one frame and therefore not be visible. Think of OnGUI as being like Update. Instead, you can use GUI.Button to toggle a state and query the state as to whether additional buttons should display or not.

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 murkantilism · Aug 04, 2012 at 08:35 PM 0
Share

Ah I see, so if I create a boolean variable and within the if(button) statement set it to true and then query that boolean for my Yes/No buttons it should work. Thank you for clearing that up, I didn't know that about GUI buttons.

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

8 People are following this question.

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

Related Questions

If statement is not working properly 1 Answer

GUI.Button with double functionality 2 Answers

GUI.Style is messing up big time 0 Answers

[FIXED] Why is my GUI.Button always pressed? 0 Answers

GUI: Overlapping and Box size collision 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