O'Reilly logo

Unity 3 Game Development HOTSHOT by Jate Wittayabundit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Casting

JavaScript automatically casts from one type to another, wherever possible. For example, the Instantiate command returns a type of Object:

JavaScript:

//There's no need to cast the result of "Instantiate" provided the variable's type is declared. 
var newObject : GameObject = Instantiate(sourceObject);

C#:

// in C#, both the variable and the result of instantiate must be declared.
// C# first version
GameObject foo = (GameObject) Instantiate(sourceObject); 
// C# second version
GameObject foo = Instantiate(sourceObject) as GameObject;

Tip

There are two different ways of casting in C#. For the first line in the preceding code, if the object can't be instantiated, it will throw an exception. You would need to use a try/catch to properly handle it. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required