Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Javascript and C#

Discussion in 'Scripting' started by eaves, Dec 25, 2010.

  1. eaves

    eaves

    Joined:
    Aug 22, 2010
    Posts:
    33
    Hello every one...

    I use javascript in my games, but i noticed that most if not all "high level" plugins and stuff are written in C#..

    so my question is, is there ANYTHING in unity that you CANT do in javascript but CAN do in C#?

    do I need to learn C# to reach a "master" level in unity? as in develop ANY kind of game? (eg. an mmorpg or something similar in complexity)

    your help will be appreciated :) and thank you all in advance ^^

    PS : I tried searching the forums for this question with no results :(
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    with unity, no.
    but when it comes to stuff on the mono / .net level, yes there are different things that UnityScript can't do.

    It does not have any consts for example, it has no by ref passing mechanisms (ref / out), and "higher end" stuff is missing as well like linq syntax, closures, ...

    since the mono 2.6 upgrade UnityScript only offers very few additional things as C# now can use the variable datatype (ie var someVar = GetComponent<MyComponent>() ) as well etc and it learned other things that are C# specific
     
  3. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    In addition to what dreamora said, UnityScript can't declare custom structs either I believe. Some other advanced C# language features aren't in UnityScript either, like extension methods, operator overriding, implicit operators, nested generics don't seem to work (not sure on this one), lambdas, type-safe delegates (not sure this one either), automatic properties, interfaces (I believe), I don't think it supports nested constructors, I think event declarations aren't supported. I've also come across a few particularly nasty compile bugs in UnityScript regarding arrays and struct properties. Finally the ability to program/compile C# in Visual Studio is a huge plus (no disrespect to MonoDevelop). Plus its absolute strict type-safeness makes developing large applications/libraries much easier.

    Ultimately, you can still develop any kind of game in either UnityScript, Boo, or C# and it mostly just matters which language you prefer. Some "master" level programmers choose C# because they like the features mentioned above, or simply choose it because that's what they're familiar with, but there are plenty "master" level UnityScript programmers too. I would say though, in general, if you're creating a huge application (MMORPG) that C# is probably the best route to go. If not for the language features, then at least for the development tools, general .NET documentation and discussions available, and for potential employees/hires as you can essentially get any seasoned C#.NET developer on board. Whereas there's no real market/education for "Unity JavaScript" developers (and you wouldn't want to hire some "website javascript developer" as they're two completely different beasts)
     
  4. PeterB

    PeterB

    Joined:
    Nov 3, 2010
    Posts:
    366
    UnityScript, from Unity 3.0, has closures, lambdas, generics and all the rest. There's no longer any reason to automatically go for C#, no matter your proficiency as a programmer. I'm a senior programmer with over 35 years of experience, and I still prefer UnityScript to C# (which to me feels rather like filling in tax returns: the sheer verbiage required to accomplish something is sometimes overwhelming), but then I do come from a background in dynamic languages, having written compilers for full implementations of Common Lisp and other languages. Let me tell you that the speed of dynamic languages doesn't need to be lower than for statically typed languages. And this is certainly true for UnityScript too.

    It's a question of preference, that's all. The only thing speaking against UnityScript is that it really isn't Javascript, which means the differences can be a little confusing. And since you really can't avoid handling built-in .NET classes anyway (hash tables, dictionaries, etc), you might want to go the C# route anyway. But if you need support for closures, which to me are terribly important if you want to design really manageable and fast modular code, you don't need to switch to C# and use delegates. Not any more:

    http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html
     
    Last edited: Jul 4, 2011
  5. freaksed

    freaksed

    Joined:
    Sep 15, 2010
    Posts:
    22
    One thing I have come across while using the EZGUI plugins is that Unityscript can't pass arguments as references. For this reason the plugin requires C#, or I suppose Boo, scripts to handle it's input delegates for speed concerns.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It can pass arguments as references, it just doesn't have the "ref" keyword. If "ref" is used in the C# function, however, the argument is passed as a reference. Same thing with "out". Otherwise a number of Unity functions wouldn't work in Unityscript, like Raycast.

    --Eric
     
  7. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    C# has the massive advantage of VS and MSDN which makes life a lot easier.

    Being strongly typed I've found it easier to debug compared to 'broken' US that relies upon var's.

    And of course, C# can be used elsewhere. E.g. I do a lot of tests by creating console applications, and my server is written in C#.

    All that said, I do use US for quick and simple scripts e.g. changing color on a object etc.

    Edit: I should note that I do use the var keyword in C# - but for clarity, not as a rule.
     
    Last edited: Jul 31, 2011
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can actually write basically the same code you can write in UnityScript also in C# without any problem, thanks to Unity 3 being C# 3.5:

    Code (csharp):
    1.  
    2. function Test()
    3. {
    4.   var a = "test";
    5.   Debug.Log("And a was " + a);
    6. }
    in C# looks like
    void Test()
    {
    var a = "test";
    Debug.Log("And a was " + a);
    }[/code]

    thanks to variable datatype (its comparable to UnityScripts var in pragma strict, so no dynamic type but a variable type thats set upon the first assigenement)