- Home /
Creating a Main Menu:
I'm having a problem with a script I'm trying to write. It's simple, but the answer's been evading me for several hours. I'm trying to create a main menu on a button click. Here's my script:
var customButton : GUIStyle; var windowRect : Rect = Rect (20, 20, 120, 50);
function OnGUI () { if (GUI.Button (Rect (1195, 6, 70, 20), "Menu", customButton)) { Menu(); } }
function WindowFunction (windowID : int) { // Draw any Controls inside the window here }
function Menu () { windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window"); }
Is there anything absurdly simple I'm missing? Anything absurdly complex? I'm trying to learn how to use the new GUI, but I'm afraid it's beyond my grasp if someone doesn't give me a hand. Thanks for any and all answers -
Elliot Bonneville
Answer by Stelimar · Mar 21, 2010 at 06:54 PM
What your code does is show the menu only when the the button has been clicked, but only for that frame. You need to set a variable when the button is clicked, then check every frame if the variable has been set, and draw the menu if it has. Something like this should work (untested):
var customButton : GUIStyle; var windowRect : Rect = Rect (20, 20, 120, 50); var showMenu : System.bool = false;
function OnGUI () { if (GUI.Button (Rect (1195, 6, 70, 20), "Menu", customButton)) { showMenu = true; } if (showMenu) { Menu(); } }
function WindowFunction (windowID : int) { // Draw any Controls inside the window here }
function Menu () { windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window"); }
You don't have to declare the boolean variable due to the automatic inference JS uses, by the way. Thanks again!
Answer by ken mcall · Mar 21, 2010 at 10:54 PM
I tried this and my script and this line,
var showMenu : System.bool = false;
wanted to be.
var showMenu : System.Boolean = false;
-kenm
Yeah, I forgot to post that I caught that. Problem solved... Thanks for the reply though.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122423im_/https://answers.unity.com/themes/thub/images/avi.jpg)