Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wapic · Dec 15, 2013 at 03:30 PM · c#guibuttonsmainmenu

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
             }
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wapic · Dec 15, 2013 at 04:16 PM 0
Share

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;
     
                     }
     
     
                 }
     
     
             }
         }
     }
avatar image Tomer-Barkan · Dec 15, 2013 at 04:26 PM 0
Share

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.

avatar image Wapic · Dec 15, 2013 at 04:34 PM 0
Share

But if i don't do that it will just overlay the other buttons and not make them dissapear

avatar image Tomer-Barkan · Dec 15, 2013 at 05:02 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hans_vesthardt98 · Dec 15, 2013 at 03:39 PM 0
Share

Sorry for the cramped up code, i don't know why it did that.

avatar image Wapic · Dec 15, 2013 at 03:54 PM 0
Share

The Application.LoadLevel(); works fine it's the Quit Game and Options that doesn't work

avatar image Commander Quackers · Dec 15, 2013 at 04:20 PM 0
Share

remember that Application.Quit(); doesnt work in the editor, only in build

avatar image Tomer-Barkan · Dec 15, 2013 at 04:24 PM 0
Share

@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...

avatar image hans_vesthardt98 · Dec 15, 2013 at 05:43 PM 0
Share

@tbkn Thank you very much! Now i know :p Always wondered lol

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges