• 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 /
This question was closed Feb 07, 2014 at 10:30 PM by Graham-Dunnett for the following reason:

Question is off-topic or not relevant

avatar image
-1
Question by Mint92 · Feb 07, 2014 at 04:34 PM · c#convert

help converting Javascript to C#

hye guys can someone help me convert javascript to c# this is c# code, but getting errors and not sure where im going wrong or need to change

 using UnityEngine;
 using System.Collections;
 
 public class Test2 : MonoBehaviour {
 
 public int [] arrayOfQuestions ;
 public Question currentQuestion;
 public bool  showQuestion = true; 
 
 class Question
 {
     private string questionText;
     private Rect questionRect;
  
     private string[] answers = new string[4];
     private string[] displayAnswers = new string[4];
     private Rect[] answerRects = new Rect[4];
  
     private string correctAnswer;
     private Rect correctRect;
  
     private int correctAnswerIndex; // Zero based
  
     private int questionWidth=300;
     private int answerWidth= 75;
     private int questionHeight= 1000;
     private int correctWidth= 500;
  
     private int selectedAnswer= -1;
  
     // Constructor
 void  Question ( Vector2 pos ,   string q ,   string a ,   string b ,   string c ,   string d ,   int correct   )
 {
        questionText = q;
        answers[0]   = a;
        answers[1]   = b;
        answers[2]   = c;
        answers[3]   = d;
  
        int st= "ABCD";
  
        for (int i= 0; i < answers.Length; i++) 
         {
             displayAnswers[i] = st[i]+": "+answers[i];   
         
  
             correctAnswerIndex = correct;
             correctAnswer = "Correct answer: " + answers[correct];
  
             CalcRects(pos);
         }
     
 
 // Calculate all the rectangles used for display
 void  CalcRects ( Vector2 pos)
 {
        Rect rect;
        rect.x = pos.x;
        rect.y = pos.y;
        rect.width = questionWidth;
        rect.height = questionHeight;
        questionRect =  rect;
  
        rect.x += questionWidth;
        rect.width = answerWidth;
        for (int= 0; i < answers.Length; i++)
     {
         answerRects[i] = rect;
         rect.x += answerWidth;
     }
  
       rect.width = correctWidth;
       correctRect = rect;
     {
 
 void DisplayQuestion ()
     {
        GUI.Label(questionRect, questionText);
        for (int i= 0; i < answers.Length; i++)
        {
             GUI.Label(answerRects[i], displayAnswers[i]);
        }
        if (selectedAnswer != -1)
        {
          GUI.Label(correctRect, correctAnswer);
        }
     }
  
     void  CheckForClick ( Vector2 pos  )
     {
        for (int i= 0; i < answers.Length; i++) 
         {
             if (answerRects[i].Contains(pos))
              {
                 selectedAnswer = i;
                 break;
             }
         }
     }
 }
  
 function Start () // This is automatically called when the game starts
 {
     arrayOfQuestions = new Question [5]; // Initalizes an array of "Questions"
  
      //Creates Question instances and places them into an array
     arrayOfQuestions[0] = new Question(Vector2(30,0),   "How many league titles have Liverpool Won?", "18", "5", "10", "9",0);
     arrayOfQuestions[1] = new Question(Vector2(30,0),  "Who won the World Cup in 2010?", "Germany", "Italy", "Brazil", "Spain", 3);       
     arrayOfQuestions[2] = new Question(Vector2(30,0),  "Who is the Manager of Manchester United?", "David Moyes", "Rafa Benitez", "Sir Alex Fergerson", "Brendan Rodgers", 0);       
     arrayOfQuestions[3] = new Question(Vector2(30,0), "The Bundesliga is a professional association football league in which country?", "Italy", "Germany", "Norway", "Austria", 1); 
     arrayOfQuestions[4] = new Question(Vector2(30,0), "Which footballer claimed that his hand-ball goal against England in 1986 was ‘The hand of God’?","Diego Maradona ", "Pele", "Wayne Rooney", "Gary Lineker", 0);   
     
     ChooseRandomQuestion(); // Picks a random question
 }
  
 void  ChooseRandomQuestion (){
     currentQuestion = arrayOfQuestions[Random.Range(0, arrayOfQuestions.Length)];
 }
   
 void  OnGUI ()
 { 
     int e= Event.current;
  
     if (e.type == EventType.Repaint)
     {
             currentQuestion.DisplayQuestion(); //Only display the currentQuestion.       
     }
  
     if (e.type == EventType.MouseDown)
     {
         foreach( Question thisQ in arrayOfQuestions )
         {
             thisQ.CheckForClick(e.mousePosition); // MINI: I didn't change this line, I'm guessing you dont need the for loop here, change it and see if it still works if you want.     
         }
     }
 }
Comment
Comments Locked · Show 3
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 Maui-M · Feb 07, 2014 at 04:37 PM 0
Share

$$anonymous$$aybe provide a screenshot of the errors?

avatar image Jessy · Feb 07, 2014 at 05:14 PM 0
Share

This is garbage. Learn to code in C# or don't use it. The community can help you learn, but it's not here to write your code for free.

avatar image robertbu · Feb 07, 2014 at 05:25 PM 0
Share

There are a number of issues with this conversion. Before you start working them through I highly recommend pulling your Question class out from inside your Test2 class. It can be at the bottom of this file, or even in a file of its own. Then work your errors one at a time from the top. That is, double click on the first error Unity lists in the Console window, fix it in $$anonymous$$onodevelop, then return to Unity to allow it to recompile. Then again fix the top error. Don't worry about the number of errors. Fixing one problem can often eli$$anonymous$$ate a dozen error messages (or not). Some specific points:

  • Pay careful attention to the indentation and the matching of braces. In $$anonymous$$onodevelop you can place your cursor nest to a '{' and it will highlight the matching '}'. $$anonymous$$ake sure you've structured your code correctly. That is the class encloses the functions. Functions do not nest. Etc.

  • Look-up how to declare a constructor. What you have here is not right.

  • The class and the methods within it will need to be public for the Question class.

  • There are cases where you've take the 'var' in the Javascript and just converted it to 'int'. You need to understand the type of the object so that you can declare it correctly.

  • You need the 'new' keyword in front of 'Vector2()' in C#

  • Lines 57 - 61 can be replaced with: 'Rect rect = new Rect(pos.x, pos.y, questionWidth, questionHeight);'

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Please Help Convert js to c# 1 Answer

this script in Javascript?? 0 Answers

Convert string to guitext, in a guitext array. 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