Csharp Differences from JS

From Unify Community Wiki
Revision as of 21:33, 21 November 2018 by Isaiah Kelly (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

End of support for UnityScript

Starting with Unity 2018.2, support for UnityScript (Unity's version of JavaScript) has ended. C# is now the only supported language going forward. You can learn more about the reasons for this change here. There is an official UnityScript-to-C# Automatic Conversion tool to help those wanting to transition their UnityScript projects over to C#.



Although the Unity Scripting page gives an overview of the differences in C# versus Javascript, it isn't comprehensive. I have run into several places where a tutorial in Javascript had some code that didn't translate directly into C#, and I had to do some research to find the C# version. So what other syntax differences are in C# versus JS?

Note: text copied from a Unity Answers question: Syntax Differences in C#/JS

Unity's Guide to C# Scripting

First, here is Unity's C# list: Writing Scripts in C#. It covers things like differences in coroutine syntax, etc.

Other Differences in C#

Function Definitions

Javascript functions are declared with the keyword function, whereas C# uses the type of the data the function is returning. A function that returns nothing is of type void, but other types such as string or float or bool are common.

function Start () { /* stuff. */ } // JS
void Start() { /* stuff. */ } // C#
string MyFunc() { return "hello, world"; } // C#
C# Generics

C# has Generic functions, which, very loosely speaking, are functions that have an attached meta-parameter to define what type of function it is. So for instance, Where the JS version is:

function Start()
  var someScript : ExampleScript = GetComponent(ExampleScript);

The C# version has the type information attached directly to the function name:

void Start()
  var someScript = GetComponent<ExampleScript>();
  // Or a more verbose version of the line above:
  var someScript = (ExampleScript) GetComponent(typeof(ExampleScript));

So you attach your component name in brackets, to the function name. Here's an example from a project where I'm adding a LevelManager GameObject. The GO has an attached script, s_LevelManager, that defines public functions, such as getMyCount():

GameObject tmp = GameObject.Find("LevelManager");
if (tmp != null) {
  s_LevelManager slm = tmp.GetComponent<s_LevelManager>();
  int count = slm.getMyCount();

Note: unity iPhone 1.6 now supports generics (it didn't before). If you want a more complete definition of Generics: MS Introduction to C# Generics

The Foreach Keyword

C# Iterators use foreach instead of for:

for(var x in someList){ // JS
  x.someVar = true;
foreach(var x in someList) { // C#
  x.someVar = true;
The New Keyword

In JS when creating a new Struct by calling its Struct name, you just use the Struct name, whereas C# requires the new keyword. Note - this includes creating new Structs inside function calls:

var foo = Vector3(0, 0, 0); // JS
var foo = new Vector3(0, 0, 0); // C#
Instantiate(someObj, new Vector3(0, 0, 0), someRotation); // C#

Speaking of Instantiate() - it requires casting to use the returned object:

var foo : GameObject = Instantiate(someObj...); // JS
var foo = Instantiate(someObj...) as GameObject; // JS
var foo = (GameObject) Instantiate(someObj...); // C#
var foo = Instantiate(someObj...) as GameObject; // C#

Note that there are two different ways of casting in C# and Javascript. For the first line above - if the object can't be instantiated, it will throw an Exception. You would need to use a try/catch to properly handle it. The second line, if it fails, will set foo to null, and not throw an Exception. Then you would just need to test if the returned object was null or not. Also, because there's the possibility for the variable to be set to null, this casting method only works for reference types; while the other method works for value types as well as reference types.

Properties with Getters/Setters

In C#, it's possible to define special functions that can be accessed as if they were variables. For instance, I could say foo.someVar = "testing";, and under the hood, there's a get and set function that process the argument "testing" and store it internally. But - they could also do any other processing on it, for instance, capitalizing the first letter before storing it. So you're not just doing a variable assignment, you're calling a function that sets the variable, and it can do - whatever functions do.

I'm not going into the syntax here, this answer is long enough :) But here are two links: MS: Using Properties and [C# Properties Tutorial]

Concluding Thoughts

One of the discussions that prompted me to compile this list, was from someone who planned to learn Javascript instead of C#, because most of the Unity tutorials were in JS. There's some merit to that, especially if you're also new to programming and are doing a lot of cut/pasting from the tutorials :) But in the long run, it's not the best reason to pick a language. The fact is, C# and Javascript are 90% identical. In most cases, only a few simple changes are needed to change a JS script to C#.

So, if they're nearly identical, why would you pick C#? That's a good way to start a religious war :) and I'm not going to try to justify one over the other here. I will say that the reason I went with C# is purely for the editor - Microsoft has a free version of Visual C# Express, which is well-integrated with Unity. It has Intellisense auto-complete, and tool-tip help that includes function parameters. Having a powerful editor really speeds up coding, and, so far anyway, I don't know of a similar (free) editor for Javascript.

Personal tools