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 BananaClipStudio · Jun 05, 2013 at 04:19 AM · javascriptongui

Two IF statements in a OnGUI function problem

I have two if statements that I have in a single OnGUI function and it seems that they cause many errors in script. Im not sure if I made a syntax error by having two if statements or if I just didnt put them in the function correctly.

 #pragma strict
 
 var triggered: boolean;
 var width  = Screen.width /2 - 100;
 var height = Screen.height / 2 - 125 ;
 var store : boolean;
 
 function OnTriggerEnter() {
 
     triggered = true;
 
 }
 
 function OnTriggerExit(){
 
     triggered = false;
     store = false;
 }
  
 
  
 
 function OnGUI() {
 
     if (triggered) {
             var textWidth = Screen.width / 2 - 75;
             var textHeight = Screen.height / 2 - 10;
             
          GUI.Label (Rect (textWidth, textHeight, 150, 20), "(E) Enter Store");
 
     }
 //    if (store) {
 //        triggered = false;
 //        
 //        GUI.Box(Rect(width, height, 200, 250), "What do you need?");
 //            
 //            if (GUI.Button(Rect(width + 50,height + 90,100,30),"Buy Store"))
 //                //This will disable the store menu and produce the buy or offer menu to buy the store
 //            
 //            if (GUI.Button(Rect(width + 50,height + 120,100,30),"Apply for Work"))
 //                //this will disable the store menu and produce the application menu for work
 //            
 //            if (GUI.Button(Rect(width + 50,height + 150,100,30),"Shop for Items"))
 //               //This will disable the store menu and produce the shopping menu
 //          
 //           if (GUI.Button(Rect(width + 50,height + 180,100,30),"Nevermind"))
 //               //this will diable the store menu and allow the player to move
 // } 
     
 }
 
 function Update(){
 
     if(triggered && Input.GetButtonUp ("e")){
         
         store = true;
         
     
                         
     }
     
 }
Comment
Add comment · Show 2
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 Benproductions1 · Jun 05, 2013 at 04:33 AM 0
Share

Can you post the errors it generates ins$$anonymous$$d of commenting out the code

avatar image BananaClipStudio · Jun 05, 2013 at 05:29 AM 0
Share

there is many but the root problem is a missing "}" on line 48 but I see one there already

1 Reply

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

Answer by robertbu · Jun 05, 2013 at 04:39 AM

Your 'if' structure here is wrong (commented out code) . Plus nested 'if(GUI.Button())' statements don't work. GUI.Button() only returns true for a single frame, so there is no way to see or interact with nested calls. You could use GUI.RepeateButton(), but you could never use the nested buttons because the moment you lift your button to press on the new buttons, they would disappear. You can have multiple 'if (GUI.Button())' statements at the top level. In order to sort these kinds of problems out, I recommend always using brackets '{}' for every 'if' and 'else' statement. Here is your code edited so that it will compile:

 #pragma strict
  
 var triggered: boolean;
 var width  = Screen.width /2 - 100;
 var height = Screen.height / 2 - 125 ;
 var store : boolean;
  
 function OnTriggerEnter() {
  
     triggered = true;
  
 }
  
 function OnTriggerExit(){
  
     triggered = false;
     store = false;
 }
  
 function OnGUI() {
  
     if (triggered) {
          var textWidth = Screen.width / 2 - 75;
          var textHeight = Screen.height / 2 - 10;
  
          GUI.Label (Rect (textWidth, textHeight, 150, 20), "(E) Enter Store");
  
     }
     if (store) {
       triggered = false;
       
        GUI.Box(Rect(width, height, 200, 250), "What do you need?");
        
        if (GUI.Button(Rect(width + 50,height + 90,100,30),"Buy Store")) {
              //This will disable the store menu and produce the buy or offer menu to buy the store
        }
             
        if (GUI.Button(Rect(width + 50,height + 120,100,30),"Apply for Work")) {
           //this will disable the store menu and produce the application menu for work
        }
         
        if (GUI.Button(Rect(width + 50,height + 150,100,30),"Shop for Items")) {
           //This will disable the store menu and produce the shopping menu
        }
       
        if (GUI.Button(Rect(width + 50,height + 180,100,30),"Nevermind")) {
               //this will diable the store menu and allow the player to move
        }
      
     }
 }
  
 function Update(){
  
     if(triggered && Input.GetButtonUp ("e")){
        store = true;
     }
  
 }
Comment
Add comment · Show 2 · 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 BananaClipStudio · Jun 05, 2013 at 05:27 AM 0
Share

im not seeing any difference besides the added curly brackets in the code. I tried to compile but it states that in line 10 there needs to be a closing "}". How do I do the get the script to work inside the GUI without nesting if statements?

avatar image robertbu · Jun 05, 2013 at 01:35 PM 0
Share

The script above compiles without errors, so if you are getting an error, you did not copy and paste correctly. As for "get the script to work," I cannot tell you exactly since I don't know your intended behavior. If you want one GUI.Button() to control other GUI elements, you will need to add a boolean value. Here is a simple example:

 #pragma strict
  
 var show = false;
 var width  = Screen.width /2 - 100;
 var height = Screen.height / 2 - 125 ;
 
 function OnGUI() {
  
    if (GUI.Button(Rect(width + 50,height + 90,150,30),"Show/Hide Other")) {
          show = !show;
    }
     
     if (show) {
         if (GUI.Button(Rect(width + 50,height + 120,150,30),"Other")) {
           // Do Other stuff
         }
     }
 }
  

 

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

15 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

Related Questions

Event.type check doesnt work inside nested list 0 Answers

Variables in GUI not updating/changing? (javascript) 1 Answer

Using Time with OnGUI Help 2 Answers

I need some help on inventory. 0 Answers

GUI text appear when mouse hovering on a object? 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