Game Development Reference
In-Depth Information
Variables with dynamically typed
resolution
Only in JavaScript, variables can have an unspecified type. This only occurs if you don't
assign a value or specify a type while declaring the variable:
// JavaScript user: The type specification is not necessary.
var playerLife : int; //statically typed (because
type specified)
var playerLife = 2; //statically typed (because
type is inferred from value
assigned)
var playerLife; //dynamically typed (because
neither a type or value
is
specified)
The dynamically typed variables will cause slower performance, and you can run into cast-
ing problems. You can use #pragma strict by including this at the top of a script to
tell Unity to disable the dynamic typing in the script and report compile errors when there
is a dynamic type in the script.
On the other hand, var in C# means that the type of variable being declared on the right-
hand side of the code will be inferred by the compiler, which will be similar to a static type
declaration. However, var cannot be used for the dynamic type:
// C# user:
var playerLife = 2; //This statement is equivalent
int playerLife = 2; //to this statement
Search WWH ::




Custom Search