Surely C#, JavaScript (sometimes referred to as UnityScript) and Boo each have their strengths. How do I find out which is most suitable for my project?
See also: Is there a performance difference between Unity's Javascript and C#?
Surely C#, JavaScript (sometimes referred to as UnityScript) and Boo each have their strengths. How do I find out which is most suitable for my project?
See also: Is there a performance difference between Unity's Javascript and C#?
I would say it depends very much on your past experience. If your background is in C++ or Java, you will likely feel most at home in C# - however if you're experienced with dynamically typed languages such as JavaScript / Basic or PHP, chances are you will prefer JavaScript.
If you have no previous experience with programming - JavaScript might be an easier introduction - given that you do not need to worry about types and casting.
Do note that writing JavaScript in Unity is not the same as writing it for web browsers (which is why it is popularly nicknamed UnityScript in the community). You should check out the tutorials to familiarise yourself with it if this is the route you're going to take.
If you are planning to work with other programmers on a Unity project, I very much recommend that you decide from the beginning to work in the same language. While it is completely possible to combine scripts written in multiple languages into one project, you will very likely at some point run into issues due to the nature of the task of compiling and linking multiple languages.
Finally do notice that all documentation and tutorials for Unity are currently written in JavaScript and of the three available languages for Unity, only C# examples are given in the .NET documentation (since you are working with mono, you have full access to the mono port of the .NET library).
Ah, thanks Lucas for this invite to flamewars :) !
After over 2 years working with Unity, I've come to the conclusion that using UnityScript is a Bad Idea. Always. I've started by using US, switched after a couple months, then a year later worked in US again with a different team... and we ended up porting all of our code to C# after 6 months (oh the joy), and fixed a billion bugs in the process.
Yes, C# has some overhead, its syntax is a tad more verbose (although you save all those useless vars and functions), and using typeof() is a bore (but as the API is ported to generics, this quickly disappears).
But US has so many shortcuts that are just invitations to make mistakes, you'll lose the time tenfold just tracking bugs! The biggest, most awful thing is implicit variable declaration. Can you spot the error in there?
var myVar;
function Update()
{
if (myvar > 0)
{
// bla bla bla...
}
}
I've mispelled myVar to myvar in Update, so the compiler will create a new local variable (that will default to 0), so the if will never happen, regarless of what you set myVar to in the editor. Happy head-banging. You will get NO WARNING WHATSOEVER from the compiler, since implicit declaration is legal (even with #pragma strict), effectively forfeiting the benefit of using a compiled scripting language instead of an interpreted one. There are tons of others pitfalls, but this one alone is worth using C#.
The number 2 reason is Visual Studio: with Intellisense and inline help, you'll be learning and discovering the API as you type. Visual C# Express is free (as in beer not speech). There is simply no better code editor on the market, at least for .NET development; if someone tells you otherwise, they're either misinformed, or fanboys of some other tool. Or just hate MS or non-open source.
Less notable: you can get lower performance if you use US the wrong way... you can prevent yourself from using it the wrong way by using #pragma strict (but by using C#, you can prevent much more!)
And lastly, with C# you have access to some .NET mechanisms than haven't been ported to US: properties, delegates, lambdas, generics... not something that will really affect you when you start programming, but once you taste their power you sorely miss them.
When I started using Unity, for ease of use, I opted for Javascript. Having come from Director Lingo, VB, ASP and some Lua I expected the learning curve to be steeper than it actually was.
Using Javascript introduced me (via tutorials and help files) to the foundations and principles Unity employs. From there it was much easier to switch over to C# since I had the engine side covered and it was just a case of figuring out the new syntax.
To me, Javascript is much easier to learn than C#. C# seemed intimidating when I started out - probably just because it had a C in the title ;) - it's also a lot stricter on the formatting, though now I've switched over I wouldn't want to go back. I'm finding it to be a real joy working with c# but I am really glad I started in javascript and eased over.
Depends on a few factors
1- Do you know any of the languages to begin with? Or if you know more than one then are there any of the languages you are more comfortable with? If so start learning using that language in the short term atleast.
2- IDE: Visual Studio is generally known as the best development IDE out there and Visual C# Express is free. I find intellisense and the features provided by C# tend to save me a lot of time and effort. I am equally comfortable in JS or C# but C# takes the cake in terms of performance and productivity. I really dont think there is much of an overhead (if any at all) associated with c#.
3- Code Examples: There tend to be more examples out there in Javascript but C# is gaining pace at good speed and I think it will take the lead in the near future specially since Visual Studio is properly supported now. I find that I have no problem translating code from an example in JS to one in C#, but if you are less comfortable with C# then you may find this a bit challenging.
4- If you find C# a bit daunting because you find C/C++ to be difficult, then you may psychologically find it harder to learn C#. I myself hate to work with C or C++ but love C# and I had no difficulty picking it up at all whereas C/C++ always gave me a lot of trouble.
I think in the short and long run it will pay off to use C#. However I know a lot comes to personal preference.
If you are new to Unity, and since all examples in the Unity documentation are currently written in Javascript, I would recommend using Javascript at least for the first few projects. Especially if you do not already prefer one of the languages over another.
Actually, Ben, your example will throw a compiler error. UnityScript does implicit variable declaration on assignment only (not in a comparison operation like that).
For the record, all of the Flashbang/Blurst games use JavaScript, and we feel this has sped up our development rather than hindered it.
I've been studying Unity since August and nearly all the basic scripting reference/tutorial/example in the indie part of the community account for Javascript. Also consider that being Unity 2.6 free now, you have your complete set of tools for free and 3D graphics is the other relevant one to learn: if you use the powerful Blender, for example, you need to put in a considerable effort. So as a beginner I kept (still keep) sticking to the basics and Javascript is the most immediate solution to ease my learning.
A very nice discussion, including a poll on who uses which language (or language combination) is available on the Unity forums: Boo, C# and JavaScript in Unity - Experiences and Opinions. In that thread, people share their experiences and backgrounds with the different languages which should help find out what's best for you.
The poll is interesting because it gives you an estimate of how many people on the forums use each language (it's certainly not representative - but should give a rough estimate).
The results as of this writing are 33% only C#, 29% only JavaScript and 27% both C# and JavaScript. Boo only is only around 4%, the rest are combinations of Boo and other languages.
The posting also has some nice links to learning resources.
Since they all relatively compile the same in the end, it's really what you can use to get what you want out of your head and into Unity. I must say however after using Boo for a while, its very disappointing that more developers don't take advantage of it. Its statically typed like C#, has even less syntax than C#, can make use of the debugger in MonoDevelop that JavaScript can't, and is all around extremely readable.
The downsides are the fact that most of the tutorials around are in C# and JS, and that it's not the best for iOS development.
I think people should take another look at Boo.
If you are just beginning programming I would definitely recommend US or if you have simple programming language backgrounds like basic or the very funny "simple programming for kids" from years ago. But C# is good if you have any other programming background.
I am learning US and when I get good and thorough with it I will switch to C#.
I started in UnityScript because it was the closest to what I knew (I was familiar with Java, Javascript, C/C++, but not C#) and most of the examples I saw were in UnityScript. So that probably helped me to get started with Unity faster, not trying to learn Unity and a new language at the same time.
But there's a lot of open-source code, .NET examples, community-donated scripts on the Unify wiki and even some of the scripts in the Unity Standard Assets are in C#, so it's a good ideal to learn that at some point. Some of my projects are in C# just because I'm including an open-source C# package.
I’ve learned to use Unity with Javascript and am now trying to learn C# and this is what I found so far : Javascript is more simple, but C# allows much more freedom. For example, you can make classes that inherit from many classes instead of just one, you can target a specific data type in your foreach statements, so let’s say you have an array with ints and floats but you only want the floats, you can just specify it, you can create personalized events, etc. So I’d sugest, learn with Javascript so you don’t overwhelm yourself, then switch to C# when you’re comfortable enough with the engine.
You shouldn’t learn any of the programming languages. In Unity, the programming languages you code in can be completely unfamiliar and you can still code in them in about 1 month. I didn’t know anything about C# but I learned it only for the purposes of unity. Try UnityScript and C# and figure out which one YOU like better. I will tell you this though, UnityScript is not JavaScript and is slower in speed comparison to C#.