- Home /
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:
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;
}
}
}
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 :)
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!
@hijinxbassist probably meant you to add the buttons again for page 2 and page 3 etc. Did you do that?
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.
@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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613072019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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