Yay, yet another C# vs. Javascript thread…
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.
) 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.
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