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. The second line, if it fails, will set foo to null, and not throw an exception. Then you would just need to test, if the returned object was null.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.134.198