• 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 Hexer · Jul 28, 2013 at 05:20 PM · c#javascriptchangeconvert

How to make a JavaScript a C# script

Hi, i have a question. how can i convert a JavaScript (or UnityScript for the community) to a C# script. This is the code i use to make my floor.

 var block : GameObject;
 
 //Change to adjust the lenght = x and z
 var worldWidth : uint  = 20; 
 var worldHeight : uint  = 20;
 
 function Start () {
  
 CreateWorld();
   
 }
   
 function Update () {
 }
  
 function CreateWorld() {
    
     //Space between each block (x)
         for(var x : uint =0; x<worldWidth; x+=1) {
        
        
      //Space between each block (z)
          for(var z : uint =0; z<worldHeight; z+=1) {
          
  
          
           var block = Instantiate(block);
           block.transform.position = new Vector3(this.transform.position.x + x, this.transform.position.y, this.transform.position.z + z);
    
          
          
           }       
           }    
           }

So, what do i have to change in the script to let this script to be a C#. I use this script to create a 20 by 20 grid out of cubes. But i wish to have this script to be C#, because some people said to me that C# runs faster most of the time.

And how do i add it so i can change the Width from x and z in the inspector?

Thanks for reading all this and i hope you can help me out.

Comment

People who like this

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

Answer by Karsnen_2 · Jul 28, 2013 at 05:26 PM

Well you can use this tool :

http://www.m2h.nl/files/js_to_c.php

It is for converting UnityScript to C#. It can only convert to a certain extent. You will probably find errors in the converted script. When you have converted, you need to manually edit the errors.

For you script this is the converted c# script. :

 // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
 // Do test the code! You usually need to change a few small bits.
 
 using UnityEngine;
 using System.Collections;
 
 public class MYCLASSNAME : MonoBehaviour {
 GameObject block;
  
 //Change to adjust the lenght = x and z
 uint worldWidth  = 20; 
 uint worldHeight  = 20;
  
 void  Start (){
  
 CreateWorld();
  
 }
  
 void  Update (){
 }
  
 void  CreateWorld (){
  
     //Space between each block (x)
         for(uint x =0; x<worldWidth; x+=1) {
  
  
      //Space between each block (z)
          for(uint z =0; z<worldHeight; z+=1) {
  
  
  
           FIXME_VAR_TYPE block= Instantiate(block);
           block.transform.position = new Vector3(this.transform.position.x + x, this.transform.position.y, this.transform.position.z + z);
  
  
  
           }       
           }    
           }
 }
Comment

People who like this

0 Show 9 · 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 Hexer · Jul 28, 2013 at 06:03 PM 0
Share

Yeah right, so i understand everything in the script but i don't know why "FIXME_VAR_TYPE" is in the script. In JavaScript was var, but i have no clue what it could be in C#. I'm missing a cast on that part.

avatar image Karsnen_2 · Jul 28, 2013 at 06:59 PM 0
Share

Well you can just delete it and with instantiate you could mention the path for instantiate your asset.

the var block and already been defined globally as gameobject block.

If you found this answer helpful kindly mark it.

avatar image AlucardJay · Jul 28, 2013 at 07:19 PM 0
Share

I personally don't think saying 'use a convertor' is a great answer. Many coders and I believe when you know how to program, you can program in all languages. It comes down to two things : logic; and syntax (the commands).

So here's a list of links I post everywhere for people wanting to convert between C# and uJS. These both taught me more about uJS and and how to read and understand C# :

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

  • http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

avatar image Hexer · Jul 28, 2013 at 07:32 PM 0
Share

Thanks for sharing the links Alucardj, I will try my best to learn and understand both language.

avatar image AlucardJay · Jul 28, 2013 at 07:40 PM 0
Share

No worries. First you get the easy rules quickly, then as you need to convert more you look it up, and slowly you see it.

Also, if you use the Unity Scripting Reference, at the above-right of every script example, there is a drop-down box. Click Javascript, then select C# to see the same example in each language.

The first thing to learn is how to declare variables :

 // uJS : var name : Type;
 // C# : Type name;

They are just the opposite way around. Something important is that in uJS vars are public by default (you can see it in the Inspector), and in C# vars are private by default, so in C# write

 public Type name;

Just go from there, you'll be reading both languages in no time.

Show more comments

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

17 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

Related Questions

C# trigger problem 1 Answer

Distribute terrain in zones 3 Answers

small example sendmessage from javascript to C# wrong??? 1 Answer

help with creating a static Instance in javascript 2 Answers

translated script from C# to JS; GUI not working properly? 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