Search Unity

C# vs. JS...a burning question

Discussion in 'Scripting' started by seejayjames, Sep 29, 2016.

  1. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    True or false: "You can do everything in Unity with JS that you can do with C#."

    I don't care if it's a lot harder to do something, or more cumbersome, or if it's better to work in C# for whatever reason, etc. I just want to know if there is anything that you absolutely CANNOT do using JS vs. C#, or vice versa. (And why, if known.)

    As stated in the docs, "Note that the API is the same regardless of which language is used, so the choice of language is purely down to preference." ... https://docs.unity3d.com/ScriptReference/index.html

    So IMO if there are things you really CAN'T do in one or the other languages, this wording really needs to be clarified.

    The reason I'm asking is because I'm going to teach a course which includes a section on Unity. Prior to that there are sections on web-based Javascript and Adobe Animate, which uses Actionscript, very similar to JS. C# is rather different in a number of ways. I'd like to keep with the same general syntax and structure for continuity. (We would use Sublime Text instead of MonoDevelop for better JS support, like formatting properly...not sure why it's not supported already, but that's a different issue I guess. In case anyone's wondering, Visual Studio has issues with the GetComponent command and likely others, wrecking all formatting following it.)

    And yes, I have read many, MANY threads about the comparisons between the languages. I haven't found a definitive answer to this specific question though.

    Thoughts?
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,145
    True, but that's because it isn't actually JavaScript. It's only a rough approximation to the actual JS language because it needs to be compatible with Mono.

    http://wiki.unity3d.com/index.php?title=UnityScript_versus_JavaScript

    Because Xamarin, the company behind Mono and MonoDevelop, did not make UnityScript. It's a language made solely by someone Unity had in their employ at one time but they never bothered to add support for the language to MonoDevelop themselves. They may have eventually added support too if C# hadn't become overwhelmingly more popular.
     
    Last edited: Sep 29, 2016
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What do you mean VS has issues with GetComponent? I have never had any issues with it.

    As far as the supported language. C# in unity is where things are going. UnityScript may always be around, but support for it is pretty much nil. JS isn't the same as UnityScript. If I were a student trying to learn scripting in Unity, I would hope my professor would do C# and not UnityScript.

    There are tons of examples in c# now and most of the tutorials I see on Unity use c# these days. Plus, Unity is working towards adding support for c# 6 and hopefully continue to maintain this support for newer versions.
     
    Last edited: Sep 30, 2016
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Half-true - they haven't added namespace support to UnityScript, have they?
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    False.
    You cannot declare jagged arrays in js
    this is fine in c#: int[][] arr = new int[N][];
    meaing you have N int arrays (of as yet un-specified length)
    you cannot do this in js, you have to use type inference, which is useless if you dont know how many N arrays you need
     
    LeftyRighty likes this.
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    I have this in VS Code:
    GetComponent. <Camera>().transform.position = Vector3(0, 0, 0);

    Which is fine, but has the extra space before <Camera>, which VS added upon format awhile back, IIRC. Works fine in Unity. If I eliminate the space, and thus make the code match the typical syntax, most of the rest of my code gets formatted as comments. Something about the dot and the angle bracket, which probably can be set in some preferences somewhere...

    I guess it's not technically an issue, but needing to keep the extra space to keep the formatting correct seems weird to me. Anyway, running some more tests here and it seems like maybe no problem.
     
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Thanks everyone for your detailed responses! The explanations and links have been incredibly helpful to sort out some questions. I of course welcome any additional thoughts and feedback.

    I think for the upcoming semester I'll stick with UnityScript for continuity in my class and see how it goes, but will go towards C# for down the road. Seems like there may be less headaches with C# as it's not a "hybrid" language trying to straddle two worlds.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,145
    Weird. I felt like I might be missing something but that's an odd one to not have access to.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    In C# there's no period before the generic argument like there is in UnityScript
    Code (csharp):
    1.  
    2. GetComponent<T> // this
    3. GetComponent.<T> // not this
    4.  
    Once the .NET upgrade goes live (experimental build out now) there'll be a lot of things you can't do because US won't support them syntactically. Lambda expressions (can't do them now and C# 6 expands on them), coalescing dot operators
     
    Ryiah and LeftyRighty like this.