Search Unity

My menu is logically showing up frame after frame but I have no idea how to destroy it!

Discussion in 'Immediate Mode GUI (IMGUI)' started by ayosha, Apr 7, 2015.

  1. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    Hello there,

    I would like to destroy my menu when the player click on "Play" (or "Load Game") but instead of that, it is launching the game but the menu stay there no matter what. I've tried to set un boolean to make it disappears and it actually disappears for less than a second, but after the next frame, it logically goes back to the initial value = true.

    Here is the code :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Menu : MonoBehaviour {
    6.  
    7.     public static bool New = false;
    8.     public static bool Load = false;
    9.     bool showButton = true;
    10.     void OnGUI () {
    11.  
    12.         if(showButton)
    13.         {
    14.         // Make a background box
    15.         GUI.Box(new Rect(Screen.width / 4, Screen.height/6, Screen.width /2 , Screen.height/1.5f), "Loader Menu");
    16.        
    17.         // New
    18.             if(GUI.Button(new Rect(Screen.width / 2.7f, Screen.height/4, Screen.width / 4, Screen.height / 10), "New")) {
    19.                 New = true;
    20.                 Load = false;
    21.             Application.LoadLevel(1);
    22.                 showButton = false;
    23.         }
    24.         // Load
    25.             if(GUI.Button(new Rect(Screen.width / 2.7f, Screen.height/2, Screen.width / 4, Screen.height / 10), "Load Game")) {
    26.                 New = false;
    27.                 Load = true;
    28.                 Application.LoadLevel(1);
    29.                 showButton = false;
    30.             }
    31.        
    32.         // Make the second button.
    33.         if(GUI.Button(new Rect(Screen.width / 2.7f, Screen.height/1.5f, Screen.width / 4, Screen.height / 10), "Quit Game"))
    34.         {
    35.             Application.Quit();
    36.            
    37.         }
    38.     }
    39.     }
    40.  
    41. }
    42.  
    Anyone has an idea ? :)
    Thanks in advance!
     
  2. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    bool showButton = true; must be treated as global static variable and initialized only globally and value changed according to onGUI events has you design. So you code looks OK except change to this:

    public static bool showButton = true;