Posted by: Morten Nobel-Jørgensen | July 7, 2010

C# versus JavaScript in Unity


If you are about to start programming in Unity game engine (unity3d.com), you have to decide if you want to use C# or JavaScript. In this blog I’ll highlight some of the important differences.

One engine – multiple languages

Unity is one of the most popular 3D indie game engines, with support for three programming languages:

  • JavaScript
  • C#
  • Boo (a python inspired languages)

One of the reasons for Unity’s success is their support for multiple languages. Today, most developers are familiar with either Java, C# and JavaScript. Java and C# are very similar, so any experienced Java programmer can easily pick up C# in a couple of days.

Unity supports this wide range of languages to target as many programmers as possible.

In the rest of the blog, I’ll focus on C# and JavaScript, since I don’t have any experience with Boo.

The platform

Unity uses the open source Mono project (a .NET implementation) for scripting support in their engine. This means that all three languages are compiled into Common Intermediate Language (CIL). The CIL bytecode are then translated into machinecode when loaded on the target platform (at least that is true for PCs).

JavaScript-ish

Unity does not really support ‘real’ JavaScript, but instead support for the JavaScript-like language UnityScript. UnityScript is like JavaScript in a lot of ways, but also have some significant changes. Most significant, UnityScript supports static typed objects, unfortunately a lot of the dynamic features of JavaScript has been removed.

This means that if you are a JavaScript programmer you still need to learn a new language.

Most of the API documentation in Unity is written for UnityScript, but since the API is exactly the same for C# it is not a big deal.

Real C#

While Unity only have pseudo JavaScript support, it does have full C# support (with advanced features such as generics and properties). This means that you can often find help with your problems outside the Unity community.

The language C# is also closer to the Common Intermediate Language than UnityScript. This means that in some (rare) situations, you are able to write code in C# that performs better than in UnityScript.

Editor support

For C# you can use Microsoft Visual Studio as an editor  for the Windows platform – or use the cross platform IDE MonoDevelop. They both supports context sensitive help and auto completion.

For UnityScript, you can use UnityDevelop for Windows (a modified FlashDevelop editor) that also support context sensitive help and auto completion.

If you for some reason prefers a plan text editor, you can use the build in Unitron editor.

Conclusion

I would recommend any programmer to use C# for Unity. In my opinion C# has the best editor support and gives the programmer most control (the price is a slightly more complex language to learn).

Unity actually support that you write some classes in C# and some classes in JavaScript – something that I would not recommend you to do.

Both languages are used a lot, so there is no wrong choice.

Advertisements

Responses

  1. Well.
    I have a Questions .I Knew that Java Scrip is working on Client and C Shap is working on the server.I don’t understand how work these on Unity.
    I am feeling confused

    • In web development, which is not Unity you are right. JavaScript is interpreted by the client browser.

      In Unity when you code your game, the JavaScript (UnityScript) gets compiled into C# which runs on the clients machine in a binary or the web player. Hope this helps.

  2. […] https://blog.nobel-joergensen.com/2010/07/07/csharp-versus-javascript-in-unity/ Share this:PrintEmailTwitterFacebookLike this:LikeBe the first to like this post. […]

  3. It’s not a surprise that javascript is modified to unityscript in this case, adding ‘typing’ means that the performance penalty in ‘boxing’ and ‘unboxing’ unknown types to prevent type errors is taken away, it’s what javascript needs to be a fast language. I’m surprised that can’t use actionscript, since Unity now has export-to-flash and actionscript has everything you need (optional typing, classes etc) maybe they’re working on it….


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: