Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED]GUI elements not clearing as anticipated.

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jonathan Cross, Oct 6, 2009.

  1. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Hello all,

    I've been fighting a GUI problem that I'm hoping you can help shed some light on what's going on. I'm working on a project that uses several scenes to manage game state. Not sure if this is the right approach for this but seemed the most reasonable. Start Game, Game Options, Play Game, etc. The problem I'm running into is that when do a build of the current game when I navigate from my start game state to the next game state (New Game) all the GUI elements (buttons,etc.) are retained from the previous state. My expectation is that the new GUI elements are show and none of the elements from the previous state should be shown. I know I'm missing something probably very simple but I cannot see it. I've searched the forums and documentation and am having no luck. Thanks in advance for the help.

    To reproduce the problem:

    • Copy the code listed below into a file:<your file>.cs.
      Create an empty GameObject in your scene hierarchy.
      Attach <your file>.cs to the empty object.
    Note that this behavior is demonstrated doing a "Build Run" for a Windows standalone. The problem does not appear when playing inside the editor.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class start_game_state : MonoBehaviour
    5. {
    6.     int m_current_state = 0;
    7.     public bool check_for_patch()
    8.     {
    9.         return false;
    10.     }
    11.  
    12.     void OnGUI()
    13.     {
    14.         switch( m_current_state )
    15.         {
    16.             case 0:
    17.                     start_game_gui();
    18.                     break;
    19.             case 1:
    20.                     new_game_gui();
    21.                     break;
    22.         }
    23.     }
    24.  
    25.     void start_game_gui()
    26.     {
    27.         Rect group_rect = new Rect(Screen.width / 2.5F, Screen.height / 2, 320, 320);
    28.         GUI.BeginGroup(group_rect);
    29.  
    30.         Rect button_rect = new Rect(0, 0, group_rect.width, 40);
    31.         if (check_for_patch())
    32.         {
    33.             if (GUI.Button(button_rect, "Update"))
    34.             {
    35.                 //Application.LoadLevel("update_game");
    36.             }
    37.             button_rect.y += 90;
    38.         }
    39.  
    40.         if (GUI.Button(button_rect, "New Game"))
    41.         {
    42.             //Application.LoadLevel("new_game");
    43.             m_current_state = 1;
    44.         }
    45.  
    46.         button_rect.y += 45;
    47.         if (GUI.Button(button_rect, "Load Game"))
    48.         {
    49.             //Application.LoadLevel("continue_game");
    50.             m_current_state = 1;
    51.         }
    52.  
    53.         button_rect.y += 45;
    54.         if (GUI.Button(button_rect, "Options"))
    55.         {
    56.             //Application.LoadLevel("game_options");
    57.             m_current_state = 1;
    58.         }
    59.  
    60.         button_rect.y += 45;
    61.         if (GUI.Button(button_rect, "Credits"))
    62.         {
    63.             //Application.LoadLevel("credits");
    64.             m_current_state = 1;
    65.         }
    66.  
    67.         button_rect.y += 45;
    68.         if (GUI.Button(button_rect, "Exit Game"))
    69.         {
    70.             //Application.LoadLevel("end_game");
    71.             Application.Quit();
    72.         }
    73.  
    74.         GUI.EndGroup();
    75.     }
    76.  
    77.     void new_game_gui()
    78.     {
    79.         Rect group_rect = new Rect(Screen.width / 2.5F, Screen.height / 2, 320, 240);
    80.         GUI.BeginGroup(group_rect);
    81.  
    82.         Rect button_rect = new Rect(0, 0, group_rect.width, 40);
    83.         if (GUI.Button(button_rect, "Play Game"))
    84.         {
    85.             m_current_state = 0;
    86.             //Application.LoadLevel("play_game");
    87.         }
    88.  
    89.         button_rect.y += 45;
    90.         if (GUI.Button(button_rect, "Back"))
    91.         {
    92.             m_current_state = 0;
    93.             //Application.LoadLevel("start_game");
    94.         }
    95.  
    96.         GUI.EndGroup();
    97.     }
    98. }
    99.  
    100.  
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Welcome to the forums! :D

    Your GUI branching behavior seems predicated on the value assigned to m_current_state, are you sure that it has the value you expect in both scenes? Specifically, are you sure that when you build your scene it is assigned a value of zero and of one in the two scenes as expected. Your instructions to reproduce the problem don't explicitly describe making two scenes, nor do they describe ensuring that the variable's value is set properly in both scenes so my gut reaction is to ask if you've done that so far. Have you? :)
     
  3. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Hey Higgy, Thanks for the reply. Yup, I verified that m_current_state is being set correctly and both the start_game_gui() and new_game_gui() are drawing their respective buttons correctly based upon that value and the switch. The problem occurs that, when run outside of the editor when the "New Game" button is pressed and the new_game_gui() is called the newer buttons are drawn over the older start_game_gui() buttons without these older buttons being cleared from the screen as I would have anticipated. When run within the editor the buttons clear from the previous menu as I would expect.

    You are correct in that this example I do not explicitly call out creating two scenes within the project. This is a simplest example I came up with that demonstrates the behavior. FWIW I have noticed this behavior when loading into another scene on an Application.LoadLevel("another_scene") where another GUI is created and the older GUI is retained in the "background". I just did not want to add complexity to this question where it may not be needed. (Especially if the solution is a simple one).

    If the scenario I'm describing still doesn't make sense I'll be happy to post some screenshots.

    Thanks for the help.
     
  4. Will Gifford

    Will Gifford

    Joined:
    Sep 7, 2009
    Posts:
    11
    I took the script that he wrote, built it into a windows binary, and I am exhibiting the same behavior. Wonder why it is overwriting the old GUI instead of clearing it. Very strange. I don't know what is going on. Bug in Unity? Anyone else seeing this?

    Windows 7 64bit
    Quad Core2 Duo 2.83GHz
    8 GB RAM
    Nvidia GeForce GTX 275
     
  5. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Bumped for even simpler code that exhibits the behavior. Still works as expected within the editor but does not as a Windows standalone binary.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class demo : MonoBehaviour
    5. {
    6.     public int m_current_state = 0;
    7.  
    8.     void OnGUI()
    9.     {
    10.         switch( m_current_state )
    11.         {
    12.             case 0:
    13.                     display_menu_A();
    14.                     break;
    15.             case 1:
    16.                     display_menu_C();
    17.                     break;
    18.         }
    19.     }
    20.  
    21.     void display_menu_A()
    22.     {        
    23.         if (GUI.Button(new Rect(0, 0, 200, 40), "A"))
    24.         {            
    25.             m_current_state = 1; // switch and display menu_C
    26.         }    
    27.        
    28.         if (GUI.Button(new Rect(0, 45, 200, 40), "B"))
    29.         {        
    30.             m_current_state = 1;
    31.         }        
    32.     }
    33.  
    34.     void display_menu_C()
    35.     {      
    36.         if (GUI.Button(new Rect(250, 0, 200, 40),"C"))
    37.         {
    38.             m_current_state = 0; // switch and display menu_A
    39.         }
    40.        
    41.         if (GUI.Button(new Rect(250, 45, 200, 40), "D"))
    42.         {
    43.             m_current_state = 0;            
    44.         }        
    45.     }
    46. }
    47.  
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hmmm... can't reproduce this for a Mac or web player build. Are you running the editor on a Windows machine?
     
  7. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Yup:

    Windows 7 64-bit
    Core2 Duo E6850 3.0
    4GB RAM
    Nvidia 8800 GTX
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    could you please test something: ensure that you put the buttons at different enough positions in the two layouts and test again.

    because at worst you could run into a mouse key repeat that directly clicks the alternatives layout button and fires it back. as it would be visible only for a single frame if at all, I highly doubt you would realize that it switched.
     
  9. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Definitely posting them in different locations on screen. I've attached a few screenshots from a standalone Windows binary build. "menu_a" is just the menu on start-up. "menu_c_incorrect" is what is displayed after invoking display_menu_C() via a button press in display_menu_A(). However, the Rect's/Buttons from display_menu_A() are still visible. They are not focusable in this context. "menu_c_correct" was a manually altered image of what I think the menu should look like instead of "_incorrect".

    Thanks for the help!
     

    Attached Files:

  10. Jonathan Cross

    Jonathan Cross

    Joined:
    Sep 6, 2009
    Posts:
    6
    Solved. My error was in the fact that the only object I had in the scene was a GameObject with only the above script attached. Once I added a camera object the removals of the "older" buttons occur as I expect them to. Like I said initially, a very simple (and noobish) mistake.
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    hehe :)

    Yeah that can bite one very badly ... without a camera, the backdrop naturally never is flushed and as such the other UI elements seem to remain there, though if you want to use them, they shouldn't react because they are physically no longer present.
     
  12. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I'm glad to read that all is well and you've sorted this out. While the reason sounds sensible enough it just wasn't on my checklist of problem spots to ask about. It is now. :)
     
  13. onecrane

    onecrane

    Joined:
    Mar 19, 2013
    Posts:
    5
    I'm very grateful this was posted and answered - I was doing the exact same thing, getting the exact same behavior, and losing the same amount of sanity and hair. I love Unity, but man, the philosophy of "why fix our bugs when we can let the community share their workarounds?" is tiring some days.

    Thanks for posting your solution - you saved me a lot of pain today!
     
  14. Pradeep-D

    Pradeep-D

    Joined:
    Sep 10, 2014
    Posts:
    1
    hi... i have been using unity for a while... and i am facing the same problem mentioned above... thing is i have a camera in the scene but eventhough when new scene loads the old scene gui remains in the background... i need to clear that off... it would be grateful if any of u provide a solution for that...