- Home /
Question by
Memorix101 · May 26, 2013 at 03:28 AM ·
c#translation
Translating JS to C# error
Hello, currently I'm translating a JavaScript to C# in Unity, but there is an error and I need your help, cause I don't know how to solve it.
This are my errors:
using UnityEngine;
using System.Collections;
public class MenuScript : MonoBehaviour {
// Array of menu item control names.
public string[] menuOptions = new string[4] {
menuOptions[0] = "Tutorial",
menuOptions[1] = "Play",
menuOptions[2] = "High Scores",
menuOptions[3] = "Exit",
};
// Default selected menu item (in this case, Tutorial).
var selectedIndex = 0;
// Function to scroll through possible menu items array, looping back to start/end depending on direction of movement.
This line is red underlined ---> void menuSelection (menuItems, selectedItem, direction){
if (direction == "up") {
if (selectedItem == 0) {
selectedItem = menuItems.length - 1;
} else {
selectedItem -= 1;
}
}
if (direction == "down") {
if (selectedItem == menuItems.length - 1) {
selectedItem = 0;
} else {
selectedItem += 1;
}
}
return selectedItem;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("down")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "down");
}
if (Input.GetKeyDown("up")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "up");
}
if (Input.GetKeyDown("space")) {
handleSelection();
}
}
void handleSelection() {
GUI.FocusControl (menuOptions[selectedIndex]);
switch (selectedIndex)
{
case 0:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 1:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 2:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 3:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
default:
Debug.Log ("None of the above selected..");
break;
}
}
void OnGUI(){
GUI.SetNextControlName ("Tutorial");
if (GUI.Button(Rect(10,70,170,30), "Button 1 (tutorial,id:0)")) {
selectedIndex = 0;
handleSelection();
}
GUI.SetNextControlName ("Play");
if (GUI.Button(Rect(10,100,170,30), "Button 2 (play, id:1)")) {
selectedIndex = 1;
handleSelection();
}
GUI.SetNextControlName ("High Scores");
if (GUI.Button(Rect(10,130,170,30), "Button 3 (high scores, id:2)")) {
selectedIndex = 2;
handleSelection();
}
GUI.SetNextControlName ("Exit");
if (GUI.Button(Rect(10,160,170,30), "Button 4 (exit, id:3)")) {
selectedIndex = 3;
handleSelection();
}
GUI.FocusControl (menuOptions[selectedIndex]);
}
}
Comment
Best Answer
Answer by robertbu · May 26, 2013 at 03:37 AM
The problem generating the errors above is that you needed to specify the type for the parameters to the method. There were a few other problems, and I fixed them so it compiles.
using UnityEngine;
using System.Collections;
public class MenuScript : MonoBehaviour {
// Array of menu item control names.
public string[] menuOptions = new string[] { "Tutorial", "Play", "High Scores", "Exit" };
// Default selected menu item (in this case, Tutorial).
int selectedIndex = 0;
// Function to scroll through possible menu items array, looping back to start/end depending on direction of movement.
int menuSelection (string[] menuItems, int selectedItem, string direction) {
if (direction == "up") {
if (selectedItem == 0) {
selectedItem = menuItems.Length - 1;
} else {
selectedItem -= 1;
}
}
if (direction == "down") {
if (selectedItem == menuItems.Length - 1) {
selectedItem = 0;
} else {
selectedItem += 1;
}
}
return selectedItem;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("down")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "down");
}
if (Input.GetKeyDown("up")) {
selectedIndex = menuSelection(menuOptions, selectedIndex, "up");
}
if (Input.GetKeyDown("space")) {
handleSelection();
}
}
void handleSelection() {
GUI.FocusControl (menuOptions[selectedIndex]);
switch (selectedIndex)
{
case 0:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 1:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 2:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
case 3:
Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
break;
default:
Debug.Log ("None of the above selected..");
break;
}
}
void OnGUI(){
GUI.SetNextControlName ("Tutorial");
if (GUI.Button(new Rect(10,70,170,30), "Button 1 (tutorial,id:0)")) {
selectedIndex = 0;
handleSelection();
}
GUI.SetNextControlName ("Play");
if (GUI.Button(new Rect(10,100,170,30), "Button 2 (play, id:1)")) {
selectedIndex = 1;
handleSelection();
}
GUI.SetNextControlName ("High Scores");
if (GUI.Button(new Rect(10,130,170,30), "Button 3 (high scores, id:2)")) {
selectedIndex = 2;
handleSelection();
}
GUI.SetNextControlName ("Exit");
if (GUI.Button(new Rect(10,160,170,30), "Button 4 (exit, id:3)")) {
selectedIndex = 3;
handleSelection();
}
GUI.FocusControl (menuOptions[selectedIndex]);
}
} // End MenuScript class
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
How to toggle the visibility of GUI Button 3 Answers
how to move object toward target without rotating it? 1 Answer