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

People who like this

0 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 instead.

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

2 Replies

· Add your reply
  • Sort: 
avatar image

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

People who like this

0 Show 0 · 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

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

People who like this

0 Show 0 · 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

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

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

help me converting js to c# 1 Answer

Talking Code 2 Answers

Can someone help me translate this to c#? 1 Answer

C# to UnityScript conversion help. 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