New to Unity, general recap and some questions.

I have made an effort to read the scripting tutorials and guides already posted.
I’m just going to recap what I’ve learned and include some basic questions I’ve not yet found an answer to.

I understand:

  • The GameObject and script hierarchy, and how to cross access them at runtime (somewhat).
  • That regardless of what language we code in, the final result is the CIL byte code.
  • C# Boo are more Explicit, JS is more Implicit, it comes down to preference.

I’ve become extremely familiar with JS for the web over the years so I just want to know where the line is drawn with Unity’s JS since it is superficial in comparison.

Is CIL ECMAScript based? Since function overloading is available, it leads me to think no, which raises other questions… Which of these is still possible? (I know some are easy to test for, but others can read this too)

  • In JS for the web I’ve become very used to dynamic arrays and non-permanently-typecast variables.

  • In cases where we know the definitive size for the array, is a faster array class available?

  • does type casting variables with <: Type> offer any run time performance benefits or is it just for development and compile benefits? (I noticed UnityJS will auto type cast variables when initialized with a value)

  • I find it handy to put relevant variables inside a function as if it’s a container (since functions are derived from the object class, this was possible with web JS).
    Example

function myFunc() { *code* }
myFunc.myVar = "relevant variable to this function";
  • JS has two ways to declare a function, are both accepted?
    Example
function myFunc(){ *code* }
// or
var myFunc = function() { *code* };
  • Is it possible to re-declare custom functions at arbitrary points in the code?
    Example
function myFunc(){ Debug.Log("version 1"); }
// later
myFunc = function(){ Debug.Log(version 2"); };

And finally, when using DontDestroyOnLoad where will that object appear in the new scene? How can we get a handle to it?

Thanks in advance.

In Unity, the only way to get a non-typecast variable is to explicitly declare it without a type:

var foo;

You rarely ever want to do that though; it’s much slower.

Yes, built-in .net arrays, such as int[ ], String[ ], float[ ], etc. are many times faster than dynamic arrays. If you don’t know the size, Unity 3 has generics in Javascript now, so you can use Lists, which are much faster than dynamic arrays, and are not dynamically typed (only the size is dynamic).

It’s a matter of preference:

var foo = Vector3(.5, .5, .5);
var foo : Vector3 = Vector3(.5, .5, .5);

What else would foo be but a Vector3? The second way seems unnecessarily redundant to me; it’s statically typed as a Vector3 either way and is exactly the same when compiled. Note that C# in Unity 3 can do the same thing:

var foo = new Vector3(.5f, .5f, .5f); // JS or C#? Both!

I’d use explicit typing when it’s not obvious:

var foo = var1 * var2; // wtf type is foo?
var foo : Vector3 = var1 * var2; // much better

In the first case, you’d have to look up var1 and var2 to have any idea what type foo is supposed to be.

In Unity, variables declared in functions are local to that function only. If you’re looking for classes:

class Foo {
    var myVar : String;
    function Bar () {
        //do something
    }
}

var foo = new Foo();
foo.myVar = "yay";
foo.Bar();

No, only the first. You can use delegates:

var myVar = Foo;
myVar();
myVar = Bar;
myVar();

function Foo () {
	print ("foo");
}

function Bar () {
	print ("bar");
}

No; maybe you want class inheritance instead?

The object isn’t destroyed, so it’s not recreated either; i.e., nothing changes. You get a handle the usual ways (GameObject.Find, static singleton, etc.)

–Eric