- Home /
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
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");
}
}
$$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.
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
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.
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
Your answer
Follow this Question
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