- Home /
GUI Box not showing up after object clicked
Hello all. I am working on a project that requires the use to select a couple of objects, then once those objects are selected a test question shows up. The question then asks the user to select one of the objects to be able to continue.
All of that works right now, but the problem occurs when the object is clicked. I want a GUI Box to appear letting the user know if he clicked on the right object or not. After that a set of navigation buttons become available and the user can continue. The code is am using is:
if(allObjectsClicked && isClicked ==false) { GUI.Box (new Rect (350,10,350,120), "Please RIGHT click the learning cube."); { if (Input.GetMouseButtonDown(1)) { if (Physics.Raycast (ray, out hit)) { if(hit.collider.gameObject.name == "learningCube2") { isClicked = true; if(isClicked) { GUI.Box(new Rect (350,10,350,120), "Correct, you may now advance."); navButtonEnabled = true; Debug.Log("Why aren't you making the GUI box?"); } }
else
{
isClicked = true;
if(isClicked)
{
GUI.Box(new Rect (350, 10, 350, 120), "Sorry, the correct answer was the cube. Click to advance.");
Debug.Log("Why are you still not making the GUI box?");
navButtonEnabled = true;
}//end isClicked
}//end else
}//end raycast hit
}//end getmousebuttondown
}//end GUI box
}//end allObjectsClicked
}//end popUpDisplayed
}//end testQuestionThree /**/
The problem is that the "Correct" or "Sorry" boxes aren't showing up, but it is printing to the debug log and the nav buttons are becoming available again. Everything runs without errors, just those two boxes won't show up.
Where are you calling this from? eg Start/Update/OnGUI. Have you tried using GUILayout to see if that works? Then you could check if gui is made but the problem is the box's rect.
It is being called from OnGUI. I have never done anything with GUILayout before, so I will have to read up on that.
O$$anonymous$$, I tried GUILayout and it didn't work either. I should have noted that I moved the GUI Box code somewhere else where I knew it would show up to make sure that it was written right and it worked.
Another note. If I turn the boxes that aren't showing up into if buttons and put the enable nav buttons and debug log in there then they won't show up, as I haven't clicked the button. However, the button does not show up so I can't click it. I have tried what I have there, getting rid of isClicked all together, just getting rid of the isClicked == false in the if condition. I can't think of anything else to try.
Another thing, Ins$$anonymous$$d of having an excessive number of If's within If's (Ifception!!) try using &&, so if(condition1 && condition2 && condition3) { dothis }
Answer by Kourosh · Apr 26, 2011 at 04:01 PM
That's because you've set conditions under which your box will appear. When you click the isClicked boolean will be true and does not match your "if" condition anymore, which requires to have the isClicked false in order to run through the block.
It works pretty much the same with or without the isClicked. The only difference is that when I do have isClicked the first GUI box disappears, which is something I would like to happen. But when I take isClicked out of the code then the line is still written to the Debug Log, the nav buttons are still enabled, but the first GUI box remains and the second is never shown. I don't know if it is being built and is only there for a split second until onGUI is called again or not though.
It's true that with this nesting, unless isClicked is set to false again elsewhere, that box is only going to be drawn for that onGUI call. GUI elements including boxes require constant calls to draw. If a box is being drawn, then it's being drawn from outside of that script, or your game is being paused, because it looks like as soon as isClicked is false, that whole script is ignored, returning false to the condition within which everything is nested.
Your answer
Follow this Question
Related Questions
Char select / GUI button question. 3 Answers
I am getting an error and cant figure out why, Help please? 1 Answer
GameObject GUI? 0 Answers
Advance when the sound is done playing 1 Answer
Get texture from gui to gameobject 1 Answer