• 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 LazyGoat · Jan 24, 2013 at 08:54 PM · c#javascriptconvert

Converting a javascript to C#

Would anyone be so kind as to help me convert this script to c#?

 var cols:int = 4;
 var rows:int = 4;
 var totalCards:int = cols*rows;
 var matchesNeededToWin:int = totalCards * 0.5;
 var matchesMade:int = 0;
 var cardW:int = 100;
 var cardH:int = 100;
 var aCards:Array;
 var aGrid:Array;
 var aCardsFlipped:ArrayList;
 var playerCanClick:boolean;
 var playerHasWon:boolean = false;
 
 
 function Start()
 {
     playerCanClick = true;
     aCards = new Array();
     aGrid = new Array();
     aCardsFlipped = new ArrayList();
     for(i=0; i<rows; i++)
     {
     aGrid[i] = new Array();
     for(j=0; j<cols; j++)
     {
     aGrid[i][j] = new Card();
     }
     }
 }
 
 function OnGUI () 
 {
     GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
     BuildGrid();
     GUILayout.EndArea();
     print("building grid!");
 }
 
 function BuildGrid()
 {
 GUILayout.BeginVertical();
 for(i=0; i<rows; i++)
 {
 GUILayout.BeginHorizontal();
 for(j=0; j<cols; j++)
 {
 var card:Object = aGrid[i][j];
 if(GUILayout.Button(Resources.Load(card.img),
 GUILayout.Width(cardW)))
 {
 Debug.Log(card.img);
 }
 }
 GUILayout.EndHorizontal();
 }
 GUILayout.EndVertical();
 }
 
 class Card extends System.Object
 {
     var isFaceUp:boolean = false;
     var isMatched:boolean = false;
     var img:String;
 function Card()
 {
 img = "robot";
 }
 }
Comment
Add comment · 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 Graham-Dunnett ♦♦ · Jan 24, 2013 at 09:30 PM 0
Share

Why do you want to convert from one language to another? You can use both Javascript and c# in a Unity project.

avatar image cdrandin · Jan 24, 2013 at 09:44 PM 0
Share

I find converting something you will need to do sometimes because I have had troubles when using multiple languages in Unity with complication errors and such. Still can't figure how to resolve that so I tend to rewrite the code or use the language ins$$anonymous$$d.

avatar image AlucardJay · Jan 24, 2013 at 10:07 PM 0
Share

Here's some links I found useful in converting between C# / JS :

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Jan 24, 2013 at 09:51 PM

I will convert a few of them as they have similar syntax

 int cols = 4;
 int rows = 4;
 int totalCards = cols*rows;
 int matchesNeededToWin = totalCards * 0.5;
 int matchesMade = 0;
 
 public void Start()
 {
     playerCanClick = true;
     aCards = new Array();
     aGrid = new Array();
     aCardsFlipped = new ArrayList();
     for(i=0; i<rows; i++)
     {
     aGrid[i] = new Array();
     for(j=0; j<cols; j++)
     {
     aGrid[i][j] = new Card();
     }
     }
 }

I think this is right.

Comment
Add comment · 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
0

Answer by Hyperion · Jan 24, 2013 at 10:02 PM

I won't convert everything for you, but I'll tell you how.

  • For everything that says var something:int=number, change that to int something=number, and the same applies to bools, floats, etc...

  • For all 'function's, change it to void.

  • Don't forget to encompass everything in a class (which I'm sure you know how to do)

  • Unfortunately, I've never tried having two classes in one script, so I don't know how to change that part to C# .

Hope this helps.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

Need help converting Javascript to C# 3 Answers

converting javascript to c# 1 Answer

java to C# conversion 1 Answer

Anyone able to convert this script to c#? 3 Answers

Does C# use #pragma strict? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges