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.

1 Answer

1

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);
 
 
 
          }       
          }    
          }
}

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.

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.

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

@alucardj Well he wanted to convert from unityscript to c#. When the question so concerned to one point & I found a resource available, I think it is useful. Unity offers both c# and Unityscript. Sometimes you tend to end up with a script on another language where you have no experience. But you might be really good in other. If that is the case, then I would mind converting the script and tinkering it, rather than learning something which would end up as time wasted from my pocket.

You have a point, I agree with that and your case is inspiring too. I never meant completely that learning a new language is a waste of time. For me, I am so found of C# after I made a switch from Javascript. Then when I see certain script in Javascript which I want it in C#, I kinda feel the need of the convertor for ease of my tasks and concentrate where I need to devote my time - which is in fact developing the product which I want to. Why do you feel C++ might fade away? Very interesting.