Javascript vs. Unityscript

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.

  • 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.

(Also, I agree that it’s not really Javascript.)

–Eric

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?

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.

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

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

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.

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

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!

Are there details on this somewhere?

So far just some posts on the beta mailing list, that I know of.

–Eric

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?

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 :slight_smile:

Classic browser JavaScript and JavaScript as used in Unity are not the same, no. The two are subsets of ECMAScript - more info here: ECMAScript - Wikipedia

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

Started a very similar for the same topi. Though, its locked now :smile:. 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

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.

This isn’t quite true. If you check out the JScript.Net wikipedia page (JScript .NET - Wikipedia), 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.

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.

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

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.