- Home /
Main menu help needed (C#)
So i am making a Main Menu in C# and it does not work properly
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour
{
private bool Options = false;
void OnGUI()
{
if(Options == false)
{
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"))
{
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);
}
}
if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-170, 100, 25), "Options"))
{
Options = true;
}
if (Options == true)
{
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;
}
}
}
}
}
so when I press Quit Game nothing happends and when I press Options it just goes black and the back button does show up. I don't know what's wrong so please help me.
Answer by Tomer-Barkan · Dec 15, 2013 at 04:07 PM
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;
}
}
}
}
Now the screen just goes black when i changed the code this
using UnityEngine;
using System.Collections;
public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour
{
private bool Options = false;
private bool quit = false;
void OnGUI()
{
if(Options == false && quit == false)
{
GUI.Box (new Rect(0,0,Screen.width, Screen.height),"$$anonymous$$ain $$anonymous$$enu");
if(GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-260, 100, 25),"Singleplayer"))
{
Application.LoadLevel("InGameOffline");
}
if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-230, 100, 25), "$$anonymous$$ultiplayer"))
{
Application.LoadLevel ("InGame");
}
if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-200, 100, 25), "Quit game"))
{
quit = true;
}
if(quit == true)
{
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 ("$$anonymous$$ain$$anonymous$$enu");
}
}
if (GUI.Button (new Rect(Screen.width/2 -50, Screen.height/2-170, 100, 25), "Options"))
{
Options = true;
}
if (Options == true)
{
GUI.Box (new Rect(0,0,Screen.width, Screen.height),"Options $$anonymous$$enu");
if(GUI.Button (new Rect(Screen.width/2 -45, Screen.height/2-260, 100, 25), "back"))
{
Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu");
Options = false;
}
}
}
}
}
because you put quit==false
in your main IF, so when you click the quit button, it turns quit to true, but it doesn't go into your main if, so it doesn't even reach the part of if (quit)
... Use my code as is, it should do what you need.
But if i don't do that it will just overlay the other buttons and not make them dissapear
I see, I thought that is what you wanted... edited answer. It's ok to check the quit in the main if, but then you have to put the quit dialog outside of the main if.
Answer by hans_vesthardt98 · Dec 15, 2013 at 03:43 PM
I am not an expert on the area, but i made a main menu like this, except for the quit game. I think you have to rearrange your levels so the Main Menu is one of the first ones, but i will link you my main menu so you can see for yourself.
void OnGUI () {
if (GUI.Button(new Rect( 20, 20, 150, 100 ), "Level 1", myStyle)){
Application.LoadLevel ("Level1");
}
if (PlayerPrefs.GetInt ("highScore1") < 5000){
GUI.Box(new Rect( 20, 140, 150, 100), "Level 2 \nScore 5000 \nin Level 1 \nto unlock!",myStyle2);
}
if (PlayerPrefs.GetInt ("highScore2") < 5000){
GUI.Box(new Rect( 20, 260, 150, 100), "Level 3 \nScore 5000 \nin Level 2 \nto unlock!",myStyle2);
}
if (PlayerPrefs.GetInt ("highScore1") > 5000){
if (GUI.Button(new Rect( 20, 140, 150, 100), "Level 2",myStyle)){
Application.LoadLevel ("Level2");
}
}
if (PlayerPrefs.GetInt ("highScore2") > 5000){
if (GUI.Button(new Rect( 20, 260, 150, 100), "Level 3",myStyle)){
Application.LoadLevel ("Level3");
}
}
}
Alright so what i have done here is that if my score in level 1 is over 5000 points, the GUI.Box will disappear and a button will be there instead, so that i can access level 2. Meaning that i have multiple levels that unlocks after how many points i get.
Sorry for the cramped up code, i don't know why it did that.
The Application.LoadLevel(); works fine it's the Quit Game and Options that doesn't work
remember that Application.Quit(); doesnt work in the editor, only in build
@hans_vesthardt98 - the cramped code was because you need an empty line to separate your regular text from your code text. Don't know why, but that's how it is...
@tbkn Thank you very much! Now i know :p Always wondered lol
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How do I make a custom font for a GUI button? C# 1 Answer
Multiple Cars not working 1 Answer
Problem with rect and touch 1 Answer
Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers