- Home /
GUI.window Not Displaying
I have what I'm assuming is a simple bug that just keep overlooking the answer. An extra set of eyes would be greatly appreciated...
My Issue: The "DisplayWinScreen" method is being called, however, the GUI.window is not displaying and I have no clue why. I have a "DisplayLoseScreen" method with identical code (minus the variables obviously), and when called that displays just fine. I've debugged the 'DisplayWinScreen' method and all debug logs show the method is being called and running properly.
The Code:
#region PRIVATE GUI Variables
//Win Screen
private int winWindowWidth;
private int winWindowHeight;
private Rect winWindowRect = new Rect(0,0,0,0);
private const int WIN_WINDOW_ID = 2;
//This example's condition
private bool conditionMet == true;
#endregion
void OnGUI ()
{
Button();
}
private void Button()
{
if(conditionMet == true)
{
DisplayWinScreen();
}
}
public void DisplayWinScreen()
{
Debug.Log("DisplayWinScreen has been called");
winWindowWidth = (Screen.width / 2);
winWindowHeight = (Screen.height / 2);
Debug.Log("WindowWidth: " + winWindowWidth + " WindowHeight: " + winWindowHeight);
winWindowRect = GUI.Window(WIN_WINDOW_ID, new Rect(Screen.width / 4, Screen.height / 4, winWindowWidth, winWindowHeight), WinScreen, "You WIN!");
Debug.Log("winWindowRect: " + winWindowRect);
}
private void WinScreen (int id)
{
}
Again, when I run my code, all three of the above debug logs return the values I would expect, so I know the code is running properly. Yet for some minor reason that I must be overlooking, the GUI.window is not displaying in the game.
Thanks for the help!
Answer by karl_ · Jan 16, 2015 at 05:10 PM
You have to place the GUI.Window()
call in your OnGUI
method, otherwise it won't get called on draw frames. Try this:
using UnityEngine;
public class MyWindow : MonoBehaviour
{
#region PRIVATE GUI Variables
//Win Screen
private int winWindowWidth;
private int winWindowHeight;
private Rect winWindowRect = new Rect(0,0,0,0);
private const int WIN_WINDOW_ID = 2;
//This example's condition
private bool conditionMet = true;
private bool doShowWindow = false;
#endregion
void OnGUI ()
{
/**
* If you click the Button and conditionMet == true, initialize window properties and set doShowWindow to true.
*/
if( GUILayout.Button("Check Condition Met") && conditionMet )
DisplayWinScreen();
if(doShowWindow)
{
winWindowRect = GUI.Window(WIN_WINDOW_ID, new Rect(Screen.width / 4, Screen.height / 4, winWindowWidth, winWindowHeight), WinScreen, "You WIN!");
}
}
public void DisplayWinScreen()
{
doShowWindow = true;
Debug.Log("DisplayWinScreen has been called");
winWindowWidth = (Screen.width / 2);
winWindowHeight = (Screen.height / 2);
Debug.Log("WindowWidth: " + winWindowWidth + " WindowHeight: " + winWindowHeight);
Debug.Log("winWindowRect: " + winWindowRect);
}
private void WinScreen (int id)
{
GUILayout.Label("I'm in the window!");
}
}
Thanks for the speedy response @karl_!
I agree your method will work (and I want to up vote it, but I don't have enough reputation), however I don't want to mark this as the 'answer' as I'd like to find the flaw in my current code structure. I like to keep my OnGUI code clean where it simply calls outside methods which contain the GUI. statements.
In my code the GUI.window call SHOULD display as the OnGUI() method will display what is in Button() method, which is calling what is displayed in the DisplayWinScreen() method (haha - I know, confusing in this description, but my code is much more complex than just this, and this hierarchy is much cleaner with 1000+ line scripts).
I have done this a thousand times in the past, and I'm even doing it in THIS particular class already for my lose screen (which is called through the OnGUI method via a second method call, and that second method calls the third DisplayLoseScreen() method which contains the GUI.window statement. And the lose screen displays perfectly WITHOUT the need for the GUI.window code being in the OnGUI method.
Worse case scenario (though it will make me cringe) I will use a boolean '_displayWinScreen' that toggles the window through an if statement in the OnGUI - it just bugs me since I know it's not necessary and I'm simply not seeing the basic flaw in my current code.
Thanks for the response though!
Oh, then your problem is the ==
where you assign condition$$anonymous$$et
. booleans initialize to false, and you're not assigning anything to it.
Haha, crap. That was a bad example line (though I set it to "true" when I defined the variable). The conditions for calling the win screen were ridiculously long so I just made up a 'condition$$anonymous$$et' variable.
The variables by which the if statement calls the DisplayWinScreen() method ARE being met, as my debug logs are displaying in the console to let me know that everything has been called properly.
Are you seeing the console logs every frame, or just once? The only thing I can think of is that condition$$anonymous$$et
is getting set to false at some point.
The logs occur once. When the "button" attached to the Button() method is pressed and the if statement runs AND is true it calls my DisplayWinScreen() method. If I press the button and my conditions haven't been met the logs do not appear. However, you gave me a fantastic idea! I will put a debug log in Update() to check if my 'conditions' are resetting AFTER the DisplayWinScreen() method is called! If so, there is my problem.
Answer by Broken Crayon Games · Jan 16, 2015 at 06:36 PM
@karl_ - you're brilliant! You provided the extra set of eyes I needed. I couldn't properly walk through troubleshoots myself, I just wasn't asking the right questions. Thank you!
Bug: DisplayWinScreen() called AND displayed. However, ONLY for the frame that the 'button' registered as pressed. This wasn't an issue for my DisplayLoseScreen() method as this was attached to a Timer() method's condition check rather than a button press's condition check.
Solution: I'm just going to concede and cringe while putting the boolean toggle variable + if statement check in OnGUI.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gui.textfield empty deletes. 0 Answers
GUI.Window only shows for one frame under all circumstances 2 Answers
How to get an array inside a simple class at Editor window and change arraysize? 0 Answers
GUI.Window is not resizing properly when resolution is being changed 1 Answer
OnGUI() and key pressing problems. 2 Answers