- Home /
can someone look at my script
Hi all,
I have a menu script in js and I have converted it over to c# using this convertor because OUYA dev is in c# and to get the controller to work i have to do it this way.
Any hoo... the js script works perfectly but the c# version gives me six errors all pointing at the "case" section. Could any of you kind folk have a look and point me in the right direction to fixing it.
The error I get on all six accounts is: Assets/Scripts/Menu.cs(45,17): error CS0266: Cannot implicitly convert type int' to
Menu.CurrentFocus'. An explicit conversion exists (are you missing a cast?)
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
GUISkin guiSkin;
AudioClip selectAudio;
int state = 0;
enum CurrentFocus {button1=1, button2=2, button3=3}
private CurrentFocus currentFocus = CurrentFocus.button1;
void Update (){
if (Input.GetKeyDown(KeyCode.Space) || OuyaInput.GetButtonDown(OuyaButton.O, OuyaPlayer.P01)) {
if(state == 0)Application.LoadLevel("Level1");
else if(state == 1)Application.LoadLevel("HowToPlay");
else if(state == 2)Application.LoadLevel("Credits");
}
if ( (OuyaInput.GetButtonDown(OuyaButton.DD, OuyaPlayer.P01)) || (OuyaInput.GetButtonDown(OuyaButton.DU, OuyaPlayer.P01)) ) {
audio.Play();
}
if (Input.GetKeyDown(KeyCode.DownArrow) || OuyaInput.GetButtonDown(OuyaButton.DD, OuyaPlayer.P01)) {
state++;
if(state > 2) state = 2;
}
if (Input.GetKeyDown(KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, OuyaPlayer.P01)) {
state--;
if(state < 0) state = 0;
}
switch (currentFocus) {
case 1:
if (Input.GetKeyDown(KeyCode.DownArrow) || OuyaInput.GetButtonDown(OuyaButton.DD, OuyaPlayer.P01)) {
currentFocus = 2;
} else if (Input.GetKeyDown(KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, OuyaPlayer.P01)) {
currentFocus = 1;
}
break;
case 2:
if (Input.GetKeyDown(KeyCode.DownArrow) || OuyaInput.GetButtonDown(OuyaButton.DD, OuyaPlayer.P01)) {
currentFocus = 3;
} else if (Input.GetKeyDown(KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, OuyaPlayer.P01)) {
currentFocus = 1;
}
break;
case 3:
if (Input.GetKeyDown(KeyCode.DownArrow) || OuyaInput.GetButtonDown(OuyaButton.DD, OuyaPlayer.P01)) {
currentFocus = 3;
} else if (Input.GetKeyDown(KeyCode.UpArrow) || OuyaInput.GetButtonDown(OuyaButton.DU, OuyaPlayer.P01)) {
currentFocus = 2;
}
break;
}
}
void OnGUI (){
GUI.skin = guiSkin;
GUI.SetNextControlName("1");
GUI.Button( new Rect(625,490,300,100), "", "button1");
GUI.SetNextControlName("2");
GUI.Button( new Rect(625,575,300,100), "", "button2");
GUI.SetNextControlName("3");
GUI.Button( new Rect(625,660,300,100), "", "button3");
switch (currentFocus) {
case 1:
GUI.FocusControl("1");
break;
case 2:
GUI.FocusControl("2");
break;
case 3:
GUI.FocusControl("3");
break;
}
}
}
Many Thanks in advance
Answer by KellyThomas · Jan 23, 2014 at 02:39 PM
This code is switching back and forth between enum
and int
types. I would recommend remaining consistent unless necessary.
The switch
could work like this:
//switch (currentFocus) {
// case 1:
switch (currentFocus) {
case CurrentFocus.button1:
I would also use the enum
when assigning values to currentFocus
:
//currentFocus = 2;
currentFocus = CurrentFocus.button2;
However the error is saying that a cast is available so I guess you could use this less clear alternative:
currentFocus = (CurrentFocus)2;
Thank you very much. Works a treat. Have a top day.