C# vs Javascript

I’m pretty sure this have been asked before, but since the search thingie didn’t come up with anything I’ll ask (again).

Which language do you think works best with Unity, C# or javascript ?

I know a bit of both but I’d like to get into one of them more seriously.
Right now I’m leaning towards C# as it’s steadly becoming the new standard language in game development, mainly thanks to xna.

both are fine but if you want to use more complex .net features (interop, generics, delegates) then you must use C#

I’ve only been mucking around with Unity for a few days and have been using C#. Based on my few days though I’d say Javascript seems to have more community support, most scripts I’ve found are written in JS and also the majority of the resource tutorials (the 3d/2d platformer etc) are also in JS.

I did however find the 2D platformer tutorial converted into C# on these forums which was awesome though :slight_smile:

Do you have the link? I can’t find it…

Is there a speed difference between optimized Unityscript and C#?

Nope; it’s all the same CIL code in the end. I prefer JS because it’s tailored to Unity, and some of the common things you do in Unity take fewer steps and are easier to deal with. It’s true that there are a few language features that C# has which JS doesn’t though.

–Eric

I’ll second that. I can program anything I put my mind to in JS. I’ve never found it limiting in any way.

http://forum.unity3d.com/viewtopic.php?t=26787&highlight=tutorial

Also I’m going to go comment on his post as it looks like nobody even replied when he posted it! I’ve already learnt so much just reading through it

C#, because it’s much easier to write bug-free code in C#. Dynamically typed languages are always a pain to debug. Catching as many bugs as possible at compile-time is very important to me.

@jbury: Thanks a lot!

@magwo: I couldn’t find it anymore, probably coming in 2.6, their’s a ‘Pragma Strict’ directive that forces the type declaration in the source. In BlitzMax this is called SuperStrict, I never worked without it, glad it’ll come in Unity too.

#pragma strict has existed since at least 2.0 :slight_smile:

But pragma strict does not make it the same as strict/superstrict in BM do. it only prevents it from changing types.
You still are not forced to define the type though, it will be assigned when you assign something to the variable for the first time, still leaving a good chance to have “some fun”

Oh ok, I tried to find it again, but the whole Paths branch has been deleted from the scripting reference again. Yesterday evening it read like I mentioned, maybe it’s a change for 2.6, we will see, but maybe I misread this part 8)

Edit: Not in a deleted part but only shown with google search, not the built in search.

There are other problems… bugs with overriden virtuals (override keyword), unreachable code, unused variables, local variable shadowing etc.

Not sure how many of these are caught by the javascript compiler.

Maybe you’re confusing static typing with type inference. Javascript is mostly strongly typed even without #pragma strict because of type inference. “var foo = 5;” makes foo statically typed as an int; there is no dynamic typing there.

Almost all of my bugs are with logic, so the issue of static typing or not is basically irrelevant as far as that goes anyway. What helps a lot more is readable code, and since Javascript simplifies Unity interaction and has less verbosity to wade through, I find it more readable, and hence easier to write less buggy code with.

Javascript doesn’t currently warn about unused variables. It used to, so I’m not sure why it doesn’t anymore. This is one thing about Javascript that quite annoys me, since that has resulted in bugs on occasion. It added unreachable code warnings a couple versions back…personally I’d rather have the unused variable warning…

–Eric

@Eric, I misread it thinking that with Pragma strict I always had to declare the type, but it actually means that when Unity cannot guess the type it wouldn’t use dynamic typing but halt with an error. So in the end I could be lazy but still don’t have to fear the slow dynamic typing since Unity would force me to then declare the type, correct now?

Correct. :slight_smile:

–Eric

superduper :stuck_out_tongue:

I started out with javascript because the documentation, all the examples and tutorials were in javascript. Then I saw that almost all user code on the forum and wiki were written in C# so I switched.

Now that I’ve used C# and Visual Studio for some time I’ll never switch back. :slight_smile: Visual Studio is one of the best applications out there and a good reason to stick with C# in my opinion.

And like the OP stated XNA uses C# and I’ve actually started making a a few games in XNA with a couple of friends.

For someone who has programmed a long time it’s probably no problem switching between many programming languages, but for one like me who has only programmed for a couple of months its nice to stick with as few as possible.

Not really…the wiki is about 50% Javascript, 35% C#, and 15% both. (Plus a few Boo scripts.) Not sure about the forum, but clearly it’s advantageous to know both JS and C#. Both are more similar to each other compared to Boo, so it’s not hard to switch between them.

–Eric