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 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: alt text

alt text

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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
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 Memorix101 · May 26, 2013 at 04:29 AM 0
Share

Thank you :)

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

14 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

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


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