- Home /
What is "(GameObject)" in "(GameObject)Instantiate(...)" for?
I came across this line of code while doing Unity's Multiplayer Networking tutorial:
var enemy = (GameObject)Instantiate(enemyPrefab, spawnPosition, spawnRotation);
What is the part "(GameObject)" called? Is there another way to write this same line of code?
Answer by phxvyper · Jun 17, 2016 at 09:02 PM
Whenever you have a type name in parentheses like that, before a value, its called an Explicit Cast.
Casting an object is when you "convert it" - for lack of a better term - to a different type that it can be converted to.
You can cast ints to floats, you can cast all types to object, you can cas List to IList, etc:
int someInt = 10;
SomeClass someObject = new SomeClass("this is a parameter");
List<string> someListOfStrings = new List<string>();
float intAsFloat = (float)someInt;
object someClassAsObject = (object)someObject;
IList stringListAsIList = (IList)someListOfStrings;
intAsFloat == 10f : true
someClassAsObject == someObject : true
stringListAsIList == someListOfStrings : true
Okay. As long as it can be converted to that type, I can use that type. Is an Explicit Cast the same as something like "Instantiate(...) as GameObject"?
If the cast succeeds, an explicit cast and a "as" cast results are the same. However, they differ if the cast fails. In that case, the explicit cast will throw an exception while the "as" cast will simply return null. There are some other nuances, but that's the important difference.
Use the "as" keyword when you want to implement further behaviour if the casting fails. Explicit casting will throw an InvalidCastException if the casting fails. But with the "as" operator you have the option to do below;
var enemy = Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject;
if(enemy == null) {
// Do something else or display a verbose debug log stating what's gone wrong
return;
}
Also note that you can't cast value types with the "as" keyword because they can't have the value "null".
@phxvyper: All your casting examples are "bad" examples for an explicit cast since they are all upcasts which are always implicit. It's not wrong to use an explicit cast here just unnecessary.
A downcast (like the one in the question) always requires an explicit cast in C#. Every class can be treated implicitly as a less defined type (base type). However a base class reference can't be treated as a more defined type without a cast. It always the same logic. Every "Dog" is an "Animal" (since Dog is derived from Animal), but not every Animal is a Dog. If you have a reference to an Animal which actually is a Dog, you need to downcast the reference explicitly to Dog in order to access Dog specific members.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Are there any draw backs to using this kind of Switch statement? 2 Answers
error ... not a UnityEngine.Component 1 Answer
Help with syntax error 1 Answer