Instantiate command returns a type of
//There's no need to cast the result of "Instantiate" provided the variable's type is declared. var newObject : GameObject = Instantiate(sourceObject);
// 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;
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
catch to properly handle it. ...