Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Javascript vs. Unityscript

Discussion in 'Scripting' started by rapiscan, Jul 18, 2010.

  1. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6
    Note: For the sake of clarity, I will use the term "Unityscript" to reference unity's version of javascript.

    As a programmer, I was initially very confused by unityscript, because I thought it was javascript (as referenced all over the website and documentation). I have since found that unityscript is much closer to JScript .Net. Along the very confusing path to enlightenment, here are the things that I found:
    • Unityscript supports both static and dynamic typing, javascript is only dynamically typed.
      [list:e0ead13ea4]
    • Unityscript supports static typing by inference, but you're also allowed to use the syntax: "var myVariable : type;" for explicit typing.
    • You can also use "#pragma strict" to force static-only typing (for performance reasons).
    [*] Javascript shares data by making variables global by default (which, admittedly, is considered a terrible mistake made by the creators of js). This doesn't work in unityscript. The "static" keyword must be used to make unityscript variables global (which isn't used at all in javascript).
    [*]Unityscript has many numerical types, while javascript has only IEEE 754 (double).
    • This caused a LOT more confusion than one might expect, since adding a float to an int will cause the float to be truncated. So when I tried to add 0.1 to 0 every frame, the result was always 0.
    [*] Unityscript has class based objects and inheritance, instead of a prototype based object model like javascript.
    [*]Unityscript does not have anonymous functions (and consequently, it does not have closures).[/list:u:e0ead13ea4]

    I am no javascript expert, and these are just my notes after one day of using unityscript. I submit that the javascript and unityscript are fundamentally different languages. In fact, the only similarity they seem to have is syntax; they are different by pretty much every other measure that is considered important in a programming language. I propose that "javascript" no longer be used to reference "unityscript". At the very least, it should be called JScript, and the documentation should make it very clear that it is not javascript.

    If that doesn't work, then I can resort to begging: Please please please please don't call it javascript; it is so completely confusing and utterly wrong.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6

    Thanks for the link, that's very useful. It never came up in all of my google searches. I really wish I had read this first or it was part of the official documentation.

    Do you know if Unity has an official stance on continuing to use the "javascript" name?
     
  4. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I agree it took me ages too to realise what was meant by Unity javascript and even more what all this .NET / mono stuff was all about. I think that should be on page one of the scripting manual.
    If you have never coded in .NET / mono / javascript you will be lost until you understand the similarities and differences.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't know, but when this has come up before, they seem attached to using "Javascript". I think it's a marketing thing...people know what Javascript is, "Unityscript" doesn't really mean anything. Unfortunately this can backfire when people (not unreasonably) expect that it actually is Javascript. It would be a really good idea to have a section in the docs explaining the language, with a brief explanation of Mono/.NET, so people know where to go for docs on that.

    --Eric
     
  6. goodhustle

    goodhustle

    Joined:
    Jun 4, 2009
    Posts:
    310
    I went through the same learning curve a while back, and it's a little tough but not too bad. Unityscript is definitely not Javascript, but it is close enough to help the transition for people who just know Javascript basics. Missing the closures really hurt, but in U3 they're going to be available! That should save some space for sure.

    Also, if you need to search for info in the future, you may want to check out this saved google search that I got from a friend:

    http://www.google.com/cse/home?cx=002470491425767499270:iugs1ezlsfq
     
  7. Derydoca

    Derydoca

    Joined:
    May 13, 2010
    Posts:
    189
    I would encourage you to use C# instead. I first started learning unity's "javascript" and having to jump back and forth from documentation and my code I decided to go with C#. It also integrates with Visual Studio really well and lets you see an object's properties with the dot operator. Another thing is that is behaves just like c#. So instead of learning a proprietary scripting language, I would just go over to c#, especially if you are developing in Visual Studio.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I wouldn't...I find Unityscript more pleasant to program Unity in. The fact that it's proprietary is an advantage: being specific to Unity means it eliminates a number of the extra hoops that C# requires you to jump through, and generally I find the syntax more reasonable and understandable. It has some drawbacks, but a number of them are addressed in Unity 3. In Unity 2.6 it's more reasonable to use C# in some cases, but in Unity 3 not so much.

    --Eric
     
  9. Derydoca

    Derydoca

    Joined:
    May 13, 2010
    Posts:
    189
    So far I have yet to see any hoops that I have to go through for C# development, but then again I am fairly new to Unity. My main reasoning with going with C# is that I program for my day job and I am always jumping from VisualBasic to C# to Javascript, and that alone is confusing. Instead of adding another language to the list of syntax I have to keep in mind, I switched over to C# and haven't looked back. If you see yourself always developing in Unity and never programming outside of it I would say stick with UnityScript. Otherwise, go with something that you can re-use. I don't want to start a flame war or anything. This is just how I see it. If UnityScript is your thing then more power to ya!
     
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Are there details on this somewhere?
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    So far just some posts on the beta mailing list, that I know of.

    --Eric
     
  12. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6
    In general, it seems quite well known that unityscript is not javascript, but it doesn't appear anywhere in the official documentation (that I can find). This really is very confusing for new developers.


    Does anyone know if there's any kind of official response from Unity on this?
     
  13. FreakFish

    FreakFish

    Joined:
    Jun 4, 2010
    Posts:
    119
    its more of a dialect of javascript really isn't it... im glad i didn't know javascript before i started (and still dont) lol :)
     
  14. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Classic browser JavaScript and JavaScript as used in Unity are not the same, no. The two are subsets of ECMAScript - more info here: http://en.wikipedia.org/wiki/ECMAScript

    The name JavaScript is indeed used for familiarity. Who here had heard the name ECMAScript prior to reading it here?
     
  15. aakaash

    aakaash

    Joined:
    Jun 28, 2009
    Posts:
    38
    Started a very similar for the same topi. Though, its locked now :D. Having 12 years of experience with javascript. Unityscript is actually driving me insane, i cant get my games tro do anything. And, i have gotten confused with the normal javascript that i have been using for 12 years in web development. someone on my topic suggested c#. any idea where i can get any tutorials or guides for the same. Thanks
     
  16. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    From the sounds of it, I have a feeling you're probably having less of an issue with syntax than you are with using the Unity API and understanding its concepts for 3D gaming. In this case, despite my previous thoughts, while you could learn C# on the side, it may overly complicate things for you.
     
  17. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6
    This isn't quite true. If you check out the JScript.Net wikipedia page (http://en.wikipedia.org/wiki/JScript_.NET), you'll see that JScript started out as Microsoft's implementation of ECMAScript, but JScript.Net has since diverged.

    The two most obvious (and very significant) differences between Unityscript and ECMAScript are the type system and anonymous functions. ECMAScript 3 (which JSCript was based on) doesn't have support for static typing or explicit type definition, while unityscript doesn't support anonymous functions. Well, these are the two differences that jumped out at me the most after using unityscript for a few days. I guess there's also the difference in the data primitives and the global scope, but those two don't seem as important.

    But this is exactly the problem! People that are familiar with Javascript think that Unityscript is javascript, but of course it isn't. Then they get confused when they try to use javascript in Unity.

    I have definitely heard of ECMA Script and I would bet nearly every professional developer has as well. It's a very prolific standard, as you can see by the list of languages in the wikipedia article that you linked to. For new programmers (that aren't familiar with ECMA script) this problem potentially even more confusing. They don't have the grounding in programming languages that a more experienced developer does, so they won't understand why learning javascript from tutorials online is different than the "javascript" in Unity.
     
  18. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6
    It looks like unityscript and JScript .Net are still quite different. For example, JScript .Net supports both class-based objects and prototype-based clsses (See
    "http://msdn.microsoft.com/en-us/library/k34df0sd(VS.71).aspx" for details) , while unityscript only supports class based objects. It looks like JScript .Net tries to be a super-set of ECMAScript, adding features but still supporting what is in the ECMAScript standard.

    It would be really nice if there was a definitive language reference for unityscript.

    EDIT: Added quotes around link to prevent the link breaking and added a little more info.
     
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's JScript, no? This is JScript.NET; note how the example compares to the old JScript 5.5, where JScript.NET doesn't use prototype and the code would work as-is in Unityscript.

    Indeed.

    --Eric
     
  20. rapiscan

    rapiscan

    Joined:
    Jul 18, 2010
    Posts:
    6
    Eric, at first I thought I was going crazy, because I was sure that I was on the JScript .Net documentation yet somehow I linked to the wrong place. Then I realized that the link is being broken by the parenthesis. If you look back at the link, then you'll see that only part of the URL is turned into a link. If you copy/paste the whole thing, it will take you to the JScript. Net documentation.

    That's really crazy that the shortened URL coincidentally links you to JScript object documentation. I tried using the BBCode URL syntax, but that doesn't work (it looks like BBCode really doesn't like parenthesis in links).

    Anyway, I updated my post and put the link in quotes, and added a little more info.
     
  21. pavees

    pavees

    Joined:
    Jan 1, 2009
    Posts:
    116
    dude actually u r working in iPhone or Web game development and wich unity are u using now. C# is not so difficult in unity as unity uses Monoframwork and mostly the .Net FrameWork is supported so it would be better to use C#to use in and also javascript is not different from the one u use in Web development but the thing is u can get the .Newt framework classes here.
     
  22. dingben

    dingben

    Joined:
    May 23, 2010
    Posts:
    45
    WOW!... refreshingly madening. I have bumped me head a few times in the last 40 years of many languages and operating systems. ...and here we go again. I have mentioned in an earlier thread that this was a marketing trick and now seeing it attributed to 'familiarity' almost makesmemadnow anew. It would not have turned me away to know the scripting language was proprietary once I started to dig into the early sections of the docs, even after having been sucked in by the 'javascript' marketing lure(brilliant I may say). Having the early sections of the doc concentrate on the language a bit and setting it in its clear perspective as compared to JScript and ECMA javascript would have been better than to let us all be blind-sided and waste much time, saving us the birds of confusion circling around our heads for the first week or 2. As concerning C#, I am having to plunge in so many C# examples that by the force of nature I am learning C# and I am now using both languages side-by-side, hoping to slowly ease into C# solely for the main reason that UScript just does not have very good tools to work with. Visual Studio is just hard to match. But I am not sure I will drop UScript completely... and yeah, bouncing from one to the other constantly does create those many moments where the brain is in C#mode and it realizes that your eyes are staring at UScript code. But I cannot get over the bad taste the lure left in me gullet. Now... here I have spoken me peace. It's all good in the end.
     
  23. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    530
    Yeah, Unity's "javascript" does smell weird... They shouldn't even call it JS... it makes as much sense as calling Boo "Python"... sure, it's a variant of the original language, but it's NOT the same...

    "The same" implies that I can use the official JS reference docs, and it would work on Unity... obviously, that isn't the case.

    I vote Unity starts calling it UnityScript officially :)

    BTW, switching to C# is probably the best idea in any case... it runs many many times faster, and coupled with VS and it's almighty Intellisense, there's really no contest ;)

    Cheers
     
  24. MegaTroll

    MegaTroll

    Joined:
    Apr 30, 2011
    Posts:
    17
    Link? DOM/Browser JS API and the Javascript Language (ECMAScript dialect) are very different things. You are literally nerfing the versatility of the word Javascript in your "smell weird" conception of the dialect.

    How did you go about your performance testing setup? Can you post the numbers?

    In any case, many would argue your first point with the fact that not having to deal with static typing is one reason enough to not get into C#.
     
  25. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It does not run any faster. It's generally the same speed; a lot of it is probably compiled to the same bytecode.

    Not really, since Unityscript is usually 99% statically typed (or 100% if your code is compliant with #pragma strict).

    --Eric
     
    Last edited: Apr 30, 2011
  26. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    530
    I've ported a procedural mesh script once from JS to C#, and was surprised to note a significant perfomance boost... I also thought that Unity would compile any script to basically the same bytecode, but it seems C# was a lot faster in that case.

    About the static typing thing, in time you see that static typing isn't a hassle at all... in fact, a large project, written entirely in JS is very much more likely to cause major headaches due to not having explicitly typed values... explicit typing makes for much better organized code... you see at first glance what a value is, and what a function does :)

    That isn't the case with UnityScript, though, which enforces typing pretty much everywhere (excepting numerical types)... But I remember a few projects I took part in, one in JS, and another in Python, where I was going mad with the lack of explicitly defined types. Inferred typing is all fun and games, until you have complex data structures and methods that require specific objects to work with...

    Cheers
     
  27. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,080
    Can you describe the procedural mesh script that received a performance boost in C# vs Unity JS?