Quantcast
Channel: Answers for "Main menu help needed (C#)"
Viewing all articles
Browse latest Browse all 4

Answer by Tomer-Barkan

$
0
0
You see, the GUI is drawn every frame in the `OnGUI()` method. So if you want it displayed, you need to call the `GUI.Button()` etc every frame. Now the problem is here: if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-200, 100, 25), "Quit game")) { GUI.Label (new Rect(Screen.width/2 -50, Screen.height/2- 220, 100, 25), "Are you sure?"); if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-200, 100, 25), "Yes")) { Application.Quit(); } if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-170, 100, 25), "No")) { Application.LoadLevel(0); } } You see, in the frame when the button is pushed, it goes into the if and desplays the are you sure message. But that is a single frame, in the next frame, the button is no longer pressed, so the dialog no longer appears. The way you can do it, is when the button is clicked, set some class variable to true, that way it remains true until you decide otherwise (until the user clicks yes or no): Here's the code: public class MainMenu : MonoBehaviour { private bool Options = false; private bool quit = false; void OnGUI() { if(!Options && !quit) { GUI.Box (new Rect(0,0,Screen.width, Screen.height),"Main Menu"); if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-260, 100, 25),"Singleplayer")) { Application.LoadLevel(2); } if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-230, 100, 25), "Multiplayer")) { Application.LoadLevel (1); } if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-200, 100, 25), "Quit game")) { quit = true; } if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-170, 100, 25), "Options")) { Options = true; } } if (quit) { GUI.Label (new Rect(Screen.width/2 -50, Screen.height/2- 220, 100, 25), "Are you sure?"); if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-200, 100, 25), "Yes")) { quit = false; Application.Quit(); } if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-170, 100, 25), "No")) { quit = false; Application.LoadLevel(0); } } if (Options) { GUI.Box (new Rect(0,0,Screen.width, Screen.height),"Options Menu"); if(GUI.Button (new Rect(Screen.width/2 -45, Screen.height/2-260, 100, 25), "back")) { Options = false; } } } }

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images