Thursday, August 8, 2013

[Info] Unity Programation: Unity JS vs C#

Hi everyone, this post has the objective to make an introduction to the both of the most used languages when you are programming in game engine Unity3D, we all know (and if you don’t, now you do) that Unity allows us to manage code in three different languages: Boo (that is a reference about Python), C# and Javascript (known as Unityscript as well).

In this post as said before, we will only be touching the both of the most popular languages, C# and Javascript, as seen in different forums, websites and so, those are most used.

First off we are going to talk about Javascript, when we are talking here about Javascript or Unityscript we are not referring the exactly same web language, but we are not getting much into this, said that, when you are just starting to use Unity this could be your best option to start with, why? I leave you these few points of why you should use Javascript when you’re just beginning:

1) Is more flexible: If you are more related in programming environment, you know that there are different types of variables for different usages, in Javascript when you are declaring a variable you can or not declare the type of variable you are using, then as the code goes, Unity will find it out but it’s own and do it for you, so if we are doing something wrong, Unity will not be such a pain and will even figuring the correct way to fix whatever is wrong (except for syntax errors).
 2)  Is a little easier to understand and manage: As not everything has to be so strict (like in C# case) you can follow a more understandable line in your code, that’s called Dynamic Typing , the only problem is that it could run just a little bit slower as Unity has to find out dynamically all the things we left in blank.
 3)  More support on Script Reference:  When you check the Script Reference everything comes in Javascript by default (coincidence?) but you can change it to either C# or Boo, but when you go deep inside the Reference you can notice that there are some features that is only documented on Javascript.

Now let’s move to C# shall we?

C# is a very known language mostly used in software such like Visual Studio, where we use it for Microsoft platforms, is nearly the same thing and it’s easier to use it when you want to use the MSDN libraries (for people who is most used to this environment), if you are more involved in programming you could use C# instead, let’s check some points:

1)  Less flexibility but more features: with C# you can manage much more data than with Javascript (thing that is being fixed lately) and you can use more fancy features using this like a bridge to communicate with some external data that Javascript could not.
 2)   More strict but more accuracy: As C# is more strict, and you have to type all the variables kind and functions, there’s no room for the error, every syntax error or some functional error on the code will be warned to you before run your code, for example when you try to convert one kind of variable to another and you do it wrong, it will throw a message to you in the console telling what are you doing wrong.
 3)  More access to System tools: This point is more for people who already is involve in programming, you can access to more advance features, like I said before, to communicate with things out of Unity.

Well now that we have seen both of them and a few of their features, some of you must be asking yourself the thing about being ‘strict’ on C# over Javascript and about typing and so, so let’s show some examples:




First you can see in the C# window (up) that there is a “Class” written right there and in the Javascript window (down) there’s not, that’s because Unity atomatically makes a hidden class out of the script name, unlike C# where you have to declare your class, that could make a harsh moment most for the new users because of that, you can’t use a script that has a different name than the name of that class we declare on the script (so forget about just copy and paste another whole script without getting an error ;) ).

Another example is in the functions we see there, we can notice in C# window that we have fist to type the return value of the function, for example void (it means that it does not return a value) or bool, then we type the name of the function (and the same for the parameter inside). It happens the same with the variable, but in Javascript we can see that for typing a variable we have just write the word var and is not necessarily to write the kind of variable we are declarating (like we did with “started” variable).

Another difference is that in Javascript the default kind of variable is a “public” variable, it means we can see the value of the variable in the Unity Inspector, to make it private (to hide it in inspector) we have to type the word “private var”, unlike C# that the default is a private variable instead of a public, and we have to type the word “public” followed by the type of value we are using on that variable.

So to end this post, let’s make ourself a question: Should I pick Javascript or C# as programming language?

Well, there is not a right answer for that question, but what I can suggest to you is if you are only beginning in all this about programming, Javascript can be a lot easier for you to start with, and it doesn’t mean you can’t start with C# as it’s only my point of view. And if you already have the knowledge of you are more involved on programming and want to do more advance features and fancy stuff, I suggest you to try C#, but the most wise practice you can do is try to take a script and translate it from one language to another, like English to Spanish and viceversa.


Well I hope this have helped you in your way to start in programming with Unity3D, keep looking at this blog to help some others (hopefully) useful tips and guides for you.

~ Sycmasters


No comments:

Post a Comment