Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
July 23, 2014
arrowPress Releases
July 23, 2014
PR Newswire
View All
View All     Submit Event





If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
The Case for C# in Unity
by Robert Plante on 02/11/13 12:50:00 am

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

 

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.

 


Related Jobs

Turtle Rock Studios, Inc.
Turtle Rock Studios, Inc. — Lake Forest, California, United States
[07.23.14]

Technical Artist - Turtle Rock Studios
Hyper Hippo Productions Ltd.
Hyper Hippo Productions Ltd. — Kelowna, British Columbia, Canada
[07.23.14]

Senior Unity Developer
Nordeus
Nordeus — Belgrade, Serbia
[07.23.14]

Senior Game Designer
Technicolor
Technicolor — Austin, Texas, United States
[07.23.14]

Core Systems Engineer






Comments


Jonathan Jennings
profile image
I started as a hobbyist with unity in javascript but since being hired as a unity engineer I have programmed exclusively in C# and I definitely have to agree that C# has definitely treated me better which makes me laugh because somehow it was easier for me to understand javascript as a weaker programmer. But I definitely feel like the strict typing is my favorite feature of C# over javascript, the way I code is usually very specific ... I want this variable of this type to do this and only this and C# lets me do that far easier than javascript in my experience.


none
 
Comment: