- Home /
boolean is true and false at a same time
with no Additional Details , this is my code
void OnGUI() {
if (ShopGui) firstInv();
if (secondGUI) secondInv();
}
void firstInv()
{
print(ShopGui);
if(ShopGui)
{
GUI.BeginGroup(new Rect(0, 0, Screen.width, Screen.height), "");
int i = 0;
for (int X = 0; X < 3; X++)
{
for (int Y = 0; Y < 3; Y++)
{
Rect slotRect = new Rect(X * ((Screen.width / 4) + 10), Y * ((Screen.width / 4) + 10), Screen.width / 4, Screen.width / 4);
if(GUI.Button(slotRect, ""))
{
Debug.Log("Clicked the button with an image");
ShopGui = false;
secondGUI = true;
}
menuSecond[i] = menuFirst[i];
GUI.DrawTexture(slotRect, menuSecond[i].view);
i++;
}
}
GUI.EndGroup();
}
}
void secondInv()
{
if(secondGUI)
{
if( GUI.Button(new Rect(50, 50, 100, 100), "SecondInv"))
{
ShopGui = true;
secondGUI = false;
}
}
}
but something strange happens , the 1st time i click on a gui .button both of my gui menus appear on screen and the shopgui boolan is both true and false.....but the next times this problems goes away... would anybody tell me why this happens
Right off the bat I'd say it's because you are not calling GUI functions within OnGUI, but I may be wrong.
Both of your method calls are inside the OnGUI so no problem there.
Do the menus stay open at the same time for long or do they just flash together momentarily?
What are the staring values to ShopGui
and secondGui
?
yes my both functions are in OnGUI and i dont think this be the reason , the first time i do single click they both appear screen ,ofcource a little pale and in my console shopGui variable is true and false and the secondgui is true , but when i double click on them i havent got this problem ,but in the next times when single click shopgui get false and secondgui true...
Answer by ham · Aug 26, 2014 at 07:00 AM
ok i think it was for changing monodevolop to VS in middle of way , i started with VS the same code and it works perfectly thanks from your comments
Answer by Kiwasi · Aug 27, 2014 at 08:08 PM
What evidence did you have that the bool is both true and false? A bool cannot be both true and false. It can change between true and false.
The most likely problem was both bools were initialised as true. This would explain why the problem only occurred on the fist instance. I'm picking you fixed this without realising it when you rewrote the script.
console was my evidence , ofcourse you are completely right but maybe because of high frame rate i saw the true and false messages together
Your answer
Follow this Question
Related Questions
UI button doesn't appear - c# 3 Answers
Boolean function doesn't work anymore 2 Answers
Problems with simple dialogue 0 Answers
OnGUI button created by a foreach loop 4 Answers