Question about Unity's JavaScript

I have decided to use Unity’s JavaScript and I would like to get a book and study more about the programming language before going through the documentation available for Unity’s JavaScript. So I have one question.

Which programming language is most similar to Unity’s version of JavaScript? Which should I learn in order to make a smooth transition to Unity’s JS.
Is it ActionScript? Javascript? Java?

I found this entry in the unifycommunity wiki and I was wondering if it’s true.

It’s most like JScript.NET, however there aren’t a lot of learning materials for that. ActionScript might be better. I don’t consider that wiki advice to be particularly relevant…if you’re looking at Java, you might as well learn C# instead.

–Eric

Thanks for the reply! Any other thoughts on this? I’d like to see more replies before I make the final decision.

Well, since you asked, I actually think C# is worth learning rather than UnityScript. It’s a little harder to pick up, but I think it’s clearer and therefore easier to learn even if it take more typing. There are lots of books and web resources on general C# which apply directly to Unity. Plus you can use it to write your own programs independent of Unity and it is almost identical to Java (not javascript), another useful language.
But if you are focussed on Unity specifically rather than learning programming maybe UScript makes sense.

Oh I think you misunderstood. I have already decided to use Unity’s JS instead of C# or Boo.

I just wanted to know which programming language is very similar to Unity’s JS and learn that before moving to Unity. Don’t get me wrong, I know one could probably start directly with Unity’s JS and learn by working through the tutorials and the documentation… but I’d like to dig in a little deeper in a similar programming language. I find this is the best way to learn.

It was the same with 3D art a couple of years ago when I started. I did learn something from the tutorials but I couldn’t reproduce anything besides those specific examples. When I got books and challenged myself, I managed to learn the tricks behind the scenes and was able to make pretty much anything. :slight_smile:

does C# makes faster builds then unityscript?

Negative.

so i will not gain anything if i learn C# instead of unityscript in term of speed?

I believe this topic should answer that question. :]

That’s not entirely true. The more explicit syntax of C# generally makes it easier to understand, maintain, debug, and extend code. Also, don’t underestimate code completion in Mono or the use of Visual Studio. If your game is complex, those could result in shipping a finished product more quickly, and that’s one form of speed.

Actionscript, ECMAScript, JScript, and JavaScript are all quite similar. Learn one, and it should be fairly trivial to learn another.

Debatable.

You’ll likely need to write less code with JS and/or Boo, and I find them both much more readable, and thus easier to understand and maintain, then C#.

Your mileage may vary of course, but that’s one of the great things about Unity… you can choose the language you like best, and there is no penalty for doing so (save missing out on iOS development if you go with Boo).

http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html

It’s also worth noting that you must perform optimizations #1 #2 on the above page for Unityscript performance to equal C# performance (since very often I see Unityscript code without these).

var isOpen: boolean; // Unityscript

bool isOpen; // C#

Anyone doing iPhone programming is already doing those things.

Yay, yet another C# vs. Javascript thread… :roll_eyes: Well, I haven’t done this in a while, so here’s why I use JS when possible:

var blah1 : int;
private var blah2 : float;

function Start () {
	yield MyFunction();
}

function MyFunction () {
	for (i = 0.0; i < 10.0; i += Time.deltaTime) {
		transform.position.x += Time.deltaTime;
		yield;
	}
}

Compare with:

using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour {

	public int blah1;
	float blah2;
	
	IEnumerator Start () {
		yield return StartCoroutine(MyFunction);
	}

	IEnumerator MyFunction () {
		for (float i = 0.0f; i < 10.0f; i += Time.deltaTime) {
			Vector3 temp = transform.position;
			temp.x += Time.deltaTime;
			transform.position = temp;
			yield return null;
		}
	}
}

First, there’s those darn “using” statements you usually have to include in C#. Useless clutter; I’m using those namespaces almost all the time anyway. Then, it always bugs me where you have to change the class name in the script whenever you rename it. The next is a matter of taste, but to me C#'s variable declaration syntax is backwards, and will always be backwards no matter how much I use it. JS is indeed more verbose here, but in a way I actually don’t mind; I find the visual “:” symbol makes it more clear at a glance what’s being done, whereas C# sort of runs together. The reversal of explicit public/private isn’t a factor for me; either one makes sense as far as I’m concerned.

Having to use “StartCoroutine” when you’re using yield is annoying…obviously it’s a coroutine, I’m yielding it. More clutter. Then there’s those annoying "f"s in floats. I just don’t like letters mixed in my numbers, sorry. The decimal makes it abundantly clear that it’s a float, and you’d rarely use doubles in Unity. I’m constantly leaving those out and having to fix it when writing C#. (Yeah, you can use those in JS now in Unity 3…not going to. :wink: ) Having to explicitly create a temporary variable when changing transform.position.x doesn’t help me, it just adds more clutter. If JS can do that busywork behind the scenes for me, good, it should. And then “yield return null”…how does that help compared to just “yield”? It doesn’t add anything useful.

For some balance, let me complain about generics in JS:

var foo = new List.<int>();

compared to C#:

var foo = new List<int>();

The syntax for generics is ugly enough to begin with, so let’s make it even more ugly! Great. :roll_eyes: It’s only an extra period, but still…why? On the other hand, you don’t have to include “import System.Collections.Generic” in JS, since it’s automatically used.

Anyway, that’s why I use JS for most Unity game code (I typically have to use C# for library code, because it has a few features I need that are still lacking in JS). It’s just simpler for me to read, write, and understand, and annoys me less overall, so I’m noticeably more productive when using it.

–Eric

Eric, your example’s more Unityscript advocacy than a balanced example, because it focuses on yield. And your knowledgeable enough I’m sure you knew exactly what you were doing! Most C# programs I’ve had the pleasure of reading use yield sparingly, less than once per couple hundred lines of code, and some eschew it entirely. If you’re going to begin a Unityscript vs C# “war”, at least begin it with a fair comparison. :wink:

Please, no C# vs JS in my topic! :smile:

Don’t drift away, I’ve already made my decision to use JS in the first post because I find it easier to the complete beginner. I’m a 3D artist and I just want to give my models a bit of color let’s say and occasionally research into a bit more advanced programming topics.

If anyone else tries to make a decision to find a similar scripting language he should find this topic in a search and give him a clear answer rather than another debate, which is what I found by searching for the answer to my question. :wink:

Please take from all these examples in both languages at least the one thing they have in common–being explicit about types is a good thing (eases debugging, may improve performance). :slight_smile:

For now I won’t be worrying about performance or other differences between using one language or the other. It seems that, the bigger the difference between them, the more advanced the topic is and I wouldn’t come across it often.
I’m gonna leave the programming tricks to the programmers. As a 3D artist, I just want to get some models in motion to present my work in a simple interactive environment. :slight_smile:

Yes, it’s advocacy; that was kind of the whole point… But it’s not focussing on yield; that’s just one element. (Although I do use yield quite a bit. When used properly it makes code much simpler…I see people getting lost in tangles of if/then statements in Update when a couple of coroutines would sort that out a lot better.) It is a fair comparison though, because it explains exactly why I prefer coding in JS and find it generally more clean and readable. Obviously a typical script isn’t going to have that many of the obvious differences compacted into that small of a script, but they still exist. It’s meant as an illustration of the things I come across in Unity programming on a consistent basis.

–Eric