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 Infectedspleen · Jan 20, 2014 at 07:31 PM · functionmenuloadlevelfocus

Adding function to my Menu Script

Hi all,

After some serious research I've created this simple menu script. It uses a GuiSkin and displays 3 buttons on the screen. I can use the Up and Down arrows to move between the buttons which highlight and when i hit space the console displays a message. So far so good.

My question would be how do i go about adding a load level function to each button so that when i move the Up down keys to choose which option i want and hit space it would load the appropriate level. I don't want to use the mouse as once this is working i can then add support for my Ouya controller.

heres my script so far...

 var guiSkin : GUISkin;
 var selectAudio : AudioClip;
 
 
 enum CurrentFocus {button1=1, button2=2, button3=3}
 private var currentFocus : CurrentFocus = CurrentFocus.button1;
 
 function Awake () {
     Screen.lockCursor = true;
 }
 
 function Update () {
     if (Input.GetKeyDown(KeyCode.Space)) {
         Debug.Log("hello world");
     }
     
     if ( (Input.GetKeyDown(KeyCode.UpArrow)) || (Input.GetKeyDown(KeyCode.DownArrow)) ) {
         audio.Play();    
     }
 
     switch (currentFocus) {
         case 1:
             if (Input.GetKeyDown(KeyCode.DownArrow)) {
                 currentFocus = 2;
             } else if (Input.GetKeyDown(KeyCode.UpArrow)) {
                 currentFocus = 1;
             }
             break;
         case 2:
             if (Input.GetKeyDown(KeyCode.DownArrow)) {
                 currentFocus = 3;
             } else if (Input.GetKeyDown(KeyCode.UpArrow)) {
                 currentFocus = 1;
             }
             break;
         case 3:
             if (Input.GetKeyDown(KeyCode.DownArrow)) {
                 currentFocus = 3;
             } else if (Input.GetKeyDown(KeyCode.UpArrow)) {
                 currentFocus = 2;
             }
             break;
     }
 
 
 }
 
 function OnGUI() {
     GUI.skin = guiSkin;
     
     GUI.SetNextControlName("1");
     GUI.Button(Rect(700,450,225,150), "", "button1");
     GUI.SetNextControlName("2");
     GUI.Button(Rect(700,575,225,150), "", "button2");
     GUI.SetNextControlName("3");
     GUI.Button(Rect(700,700,225,150), "", "button3");
     
     
     switch (currentFocus) {
         case 1:
             GUI.FocusControl("1");
             break;
         case 2:
             GUI.FocusControl("2");
             break;
         case 3:
             GUI.FocusControl("3");
             break;
             
     }
     
 }

As always, many thanks in advance

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 YoungDeveloper · Jan 20, 2014 at 07:48 PM

You could create an array of 3 states, simple integer, or maybe use switch and case, there are many possibilities. Here's a simple solution.

 var state : int = 0;
 
 function Update(){
 
     //You can highlight gui button according to state.
 
     if (Input.GetKeyDown(KeyCode.DownArrow)){
         state++;
         if(state > 2) state = 2;
     }
 
     if (Input.GetKeyDown(KeyCode.UpArrow)){
         state--;
         if(state < 0) state = 0;
     }
 
     //on enter{
          if(state == 0)Application.LoadLevel("lvl1");
          else if(state == 1)Application.LoadLevel("lvl2");
          else if(state == 2)Application.LoadLevel("lvl3");
     }
 }
Comment
Add comment · Show 3 · 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 Infectedspleen · Jan 20, 2014 at 07:57 PM 0
Share

$$anonymous$$any thanks. Will look into this once the family is in bed. But looking at the codec, I think I get it. Will let you know how I get on.

avatar image Infectedspleen · Jan 20, 2014 at 08:08 PM 0
Share

Works perfectly. Thank you. When i select an option, it script works and the level is loaded but i get an error" !dest.m_$$anonymous$$ultiFrameGUIState.m_Named$$anonymous$$eyControlList.

Is this something i can ignore.

Thank you again

avatar image YoungDeveloper · Jan 20, 2014 at 09:44 PM 0
Share

I get those errors too sometime and i am not sure what causes them, but it is unity engine related not your script. I usually just clear error log and they are gone.

avatar image
0

Answer by Jake_Frog · Jan 20, 2014 at 08:40 PM

Your buttons need to be inside of if statements.`if(GUI.Button(Rect(700,450,225,150), "", "button1")){}` Inside of the if statement, use the built in function Application.LoadLevel. Use this as a reference: http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html

Comment
Add comment · Show 1 · 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 Infectedspleen · Jan 20, 2014 at 08:41 PM 0
Share

Thank you. Got it sorted now.

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

20 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 avatar image

Related Questions

LoadLevel to menu error 0 Answers

Unity crash, no one is call the function but it show up to the party anyways 0 Answers

split-screen 'ready up' game start screen 0 Answers

How can I clear the menu buttons when clicking a button that loads another screen? 0 Answers

Photon stays in "connecting" status 0 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