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
0
Question by Broken Crayon Games · Jan 16, 2015 at 04:22 PM · onguigui.window

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!

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
0

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!");
         }
 }
Comment
Add comment · Show 6 · 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 Broken Crayon Games · Jan 16, 2015 at 05:41 PM 0
Share

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!

avatar image karl_ · Jan 16, 2015 at 05:47 PM 0
Share

Oh, then your problem is the == where you assign condition$$anonymous$$et. booleans initialize to false, and you're not assigning anything to it.

avatar image Broken Crayon Games · Jan 16, 2015 at 06:18 PM 0
Share

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.

avatar image karl_ · Jan 16, 2015 at 06:21 PM 0
Share

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.

avatar image Broken Crayon Games · Jan 16, 2015 at 06:29 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

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


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