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
1
Question by Oakie · May 27, 2012 at 05:49 PM · if statementguibutton

GUI Button within if statement doesn't disappear even when the if statement is false

So I'm trying to have this book that the player can flip through by pressing two different GUI buttons, one indicating right, the other left. Here is the code I have thus far:

     if (page1 == true && page2 == false && page3 == false && page4 == false && page5 == false){


   if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent(">"))){
 
     page2 = true;
     page1 = false;
     bookmark.renderer.enabled = false;
   }
 }
 
 if (page2 == true && page1 == false && page3 == false && page4 == false && page5 == false){
     Debug.Log("Page 2 is true");   
   /*if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent(">"))){ 
      page3 = true;
      page2 = false;
  }*/
   if (GUI.Button(Rect((Screen.width/100)*40, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent("<"))){
     page1 = true;
     page2 = false;
     bookmark.renderer.enabled = true;
  }}

The debug log indicated that it Page 2 does become true (it is initialized as false, so when you press the button it does make it true) but nothing else under the page2 if statement is run apparently.

Thanks in advance for any help!

EDIT MAY 30th: Here's a screenshot of it: alt text

And this is the current code:

   if (cameraBook.enabled == true){
  
  
  //keep track of which is the current page
   var page : int = 1;
  
   var showLog : boolean = true;
  
 
   //this makes sure that this zoom out button only shows up when you're not looking at the bookmark
   if (zoomBookmark.bookmark == false){
 
      if (GUI.Button(Rect((Screen.width/100)*25, ((Screen.height/100)*60), Screen.width / 14, Screen.height/8), GUIContent("Zoom out"))){
  
          cameraTable.enabled = true;
          cameraBook.enabled = false;
       }
       
       if (page > 0){
          if (GUI.Button(Rect((Screen.width/100)*40, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent("<"))){
            page--; //one page back 
            showLog = true;            
          }
       }
       
       if (page < 6){
         if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width/16, Screen.height/12), GUIContent(">"))){
           page++; // one page forward 
           showLog = true;           
         }
      }
  
       switch(page) //variable to switch
        {
        
          case 0 : //page = 0 (I know that's not following the whole natural numbers thing... but don't worry about it
            if (showLog){
              Debug.Log("Vieiwing page 0");
              showLog = false;
            }
            
            bookmark.renderer.enabled = true;
            musicSheet.renderer.enabled = false;
          case 1 : //page = 1
  
          if (showLog) //show the log for one frame, otherwise you print 2+ times per frame
           {
             Debug.Log("viewing page 1");
             showLog = false;
            }
       
                bookmark.renderer.enabled = false;
            
            break;
   
            case 2: // on page 2
   
             if(showLog){
               Debug.Log("Viewing Page 2");
               showLog = false;         
              }
            bookmark.renderer.enabled = false; 
                                                                                                
            break;
            
            case 3:
              if(showLog){
                 Debug.Log("Viewing Page 3");
                 showLog = false;
              }
            break;
            
            case 4: 
              if(showLog){
                 Debug.Log("Viewing Page 4");
                 showLog = false;
              }
            break;
            
            case 5:
              if(showLog){
                 Debug.Log("Viewing Page 5");
                 showLog = false;
              }
            break;
            
            case 6:
              if(showLog){                 
                 Debug.Log("Viewing Page 6");
                 showLog = false;
              }
              
             musicSheet.renderer.enabled = true;
            break;

         } 
         }    
         }

Screen shot 2012-05-30 at 10.38.48 PM.png (333.5 kB)
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
4
Best Answer

Answer by hijinxbassist · May 28, 2012 at 05:56 AM

To make your code much neater, use a switch case. You wont need all those crazy booleans ;p and it will be much easier to read. Basically it switches between the sections of code based on the case presented. I will demonstrate with an int, which will be the page number. Then each case will be the current page.

 var page:int;   //page number
 var scnH:float; //screen height
 var scnW:float; //screen width
 
 private var showLog:boolean=true;
 
 function OnGUI()
 {
     switch(page) //variable to switch
     {
         case 0:  //page = 0
 
            //This is the default value, maybe the books cover??
 
         break;
         case 1:  //page = 1
 
             if(showLog) //show the log for one frame, otherwise you print 2+ times per frame
             {        
                 Debug.Log("Viewing Page 1");
                 showLog=false;
             }
             if( GUI.Button(Rect(scnW/100*60,scnH/100*85,scnW/16,scn/12),GUIContent(">")) )
             {
                 page=2;
                 bookmark.renderer.enabled = false;
                 showLog=true;
             }
 
         break;
         case 2:  //page = 2
 
             if(showLog)
             {    
                 Debug.Log("Viewing Page 2");
                 showLog=false;
             }
             //Add the other buttons here
 
         break;
     }
 }

Not sure what the problem was..., either way this will make it much easier to control your code. With a 20 page book, you wont need 20 booleans and a page long conditional :)

Comment
Add comment · Show 8 · 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 Oakie · May 29, 2012 at 11:49 PM 0
Share

Thank you! It still doesn't quite fix the problem, I'm getting the same result in-game, but it definitely makes my code a lot cleaner and easier to read, which will certainly help in figuring out how to fix it! Thanks! I'm new to this so I totally forgot about switches. Also, I would upvote your answer if I could, sorry!

avatar image whydoidoit · May 30, 2012 at 12:06 AM 0
Share

@oakie I did it for you :)

avatar image whydoidoit · May 30, 2012 at 12:08 AM 0
Share

@hijinxbassist probably meant you to add the buttons again for page 2 and page 3 etc. Did you do that?

avatar image Bunny83 · May 30, 2012 at 01:40 AM 1
Share

Btw. your buttons looks like navigation buttons, so they are the same for each page. The only thing you have to watch out is when you view the first page to remove the back-button and when viewing the last page remove the next-button:

 // Outside of the switch statement:
 if (page > 0)
 {
     if (GUI.Button(Rect((Screen.width/100)*40, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent("<")))
     {
         page--; // one page back
     }
 }
 if (page < pagecount)
 {
     if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent(">")))
     {
         page++; // next page
     }
 }

You should also be careful that interactive GUI elements don't overlap.

avatar image hijinxbassist · May 31, 2012 at 03:19 AM 1
Share

@oakie Wait, are those variables (page & showLog) in OnGUI() ???? Those should be declared uptop. Thats where the problem is currently. Took a second to realize it, make them private vars at the very top and you should be good to go. Right now each time OnGUI gets called, page=1 thats why it goes back to page 0.

Show more comments
avatar image
1

Answer by You! · May 30, 2012 at 02:05 AM

Your real problem was that you "commented out" your code block for the stuff you wanted to do. This...

        if (page1 == true && page2 == false && page3 == false && page4 == false && page5 == false){
     // [...]
 
     if (page2 == true && page1 == false && page3 == false && page4 == false && page5 == false){
         Debug.Log("Page 2 is true");   
       /*if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent(">"))){ 
          page3 = true;
          page2 = false;
      }*/
       // [...]
 

should actually be this...

 if (page1 == true && page2 == false && page3 == false && page4 == false && page5 == false){
     // [...]
 
     if (page2 == true && page1 == false && page3 == false && page4 == false && page5 == false){
         Debug.Log("Page 2 is true");   
       if (GUI.Button(Rect((Screen.width/100)*60, ((Screen.height/100)*85), Screen.width / 16, Screen.height/12), GUIContent(">"))){ 
          page3 = true;
          page2 = false;
      }
       // [...]

Note: the "// [...]" means part of the script is skipped.

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 Oakie · May 30, 2012 at 05:24 AM 0
Share

Thanks! Yeah that was dumb of me to upload the code without taking the comments out- but I had commented it out to see if the button was still there even if Debug.Log was saying it was on page 2, which it was. So the comment was an intentional thing. Totally my bad for not uncommenting it before copying and pasting it though.

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

6 People are following this question.

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

Related Questions

GUI textfield duplicate text 0 Answers

GUI.Button not responding when drawn in a Rect. 2 Answers

[C#] if statement HELP! 1 Answer

If statment not printing to console. 2 Answers

Unity string comparison not working... 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