Sponsored By

The Case for C# in Unity

When first coming to Unity there are three different languages available to the developer, and it can be hard to choose between Javascript, C#, and Boo. Here's why you should choose C# to work with.

Robert Plante, Blogger

February 11, 2013

4 Min Read
Game Developer logo in a gray background | Game Developer

 

I would like to take this post to explain why using C# in Unity is a better idea than Javascript. If you end up not needing multiple returns for functions, non-type-specific logic, or are okay with the wonky auto-completion for Javascript, then that is okay. On the other hand, if you want to take advantage of those features, and more read on.

C# and Javascript are different, but similar. If you are coming from a C++ background (like myself), then you may find C# more familiar to begin with. Below is a quick list of what I will be covering that makes C# more friendly.

  1. Strict typing

  2. Parameters as reference (a.k.a. multiple function returns)

  3. Generic Functions (a.k.a. type independent logic)

  4. Multidimensional Arrays

  5. Full access to C# features*

  6. Auto-completion

  7. Unity is built with it

*Some newer features of C# are not supported because Unity compiles the code itself

First we will cover C# strict typing requirement. You may be wondering why having to declare access level and type for everything is a good thing. Not only will you need to do it when coding professionally for a back-end, but explicitly declaring types has two major benefits. The first is that it will always every be one thing, and so you can't accidentally shoot yourself in the foot at runtime. Secondly, having an explicit type will help to reduce memory costs. This is because the type is known and does not need to fluctuate.

There are two ways to pass parameters to a function by reference in C#. These are ref and out. The main difference between ref and out is that out does not need to be declared before it is passed to the function. In either case, when a function modifies a value that is passed in by reference, that changer persists outside the function. This lets one function "return" multiple values while keeping the return open for other indications.

Generic functions are awesome, if you have ever been in a situation where you are copy pasting code, but only changing the calling script, then generic functions are for you. They take the form of public void Foo(T param) and can include the statement where T : BaseClass before the opening braces to limit what types are allowed. MSDN has some good documentation on what makes generics awesome. Then, since you know the shared base class contains the function definition, you can call the function once with the referenced script instead of having to copy paste your logic tree and change up who calls it. Remember, if you are copy pasting in code, then chances are you can do it a better way.

Another really big thing that C# has over Javascript in Unity is multidimensional arrays. Or, more simply, arrays of arrays. This is incredibly useful for any number of reasons, 2D, 3D and 4D grids for example. Depending on your game you may not need it, but having it can certainly be helpful.

Unity makes Javascript support a number of handy features such as true Object Oriented Programming and polymorphism. These and other handy features are natively supported in C#. For a complete breakdown of C#, I strongly suggest looking at the MSDN documentation.

You may think that auto-completion within MonoDevelop is rather trivial. I did too until it started getting in the way of my coding. One perfect example is for (var i : int = 0; i < something; i++). What's wrong with that? Start typing it in Javascript, and this will happen: for (var i : int = 0; int < right there it autocompleted i to int. When this happens enough, it becomes very very frustrating. There are a number of other cases where this happens, or some variables just aren't indexed, but so far in C# I haven't run into these problems.

Finally, Unity was built in C#. This means that C# code will integrate natively. While the Javascript is compiled resulting in very little to no difference in terms of cost, C# has just given me the better treatment all around. Then again I'm coming from a C++ background, so your experiences may differ.

 

Read more about:

2013Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like