- Home /
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.
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);
}
}
}
}
Yeah right, so i understand everything in the script but i don't know why "FIX$$anonymous$$E_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.
I personally don't think saying 'use a convertor' is a great answer. $$anonymous$$any 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# :
Thanks for sharing the links Alucardj, I will try my best to learn and understand both language.
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.
Your answer
Follow this Question
Related Questions
C# trigger problem 1 Answer
translated script from C# to JS; GUI not working properly? 1 Answer
java to C# 2 Answers
Multiple Cars not working 1 Answer
How to make this line work in C#? 1 Answer