Instantiate with and without casting as GameObject
Hey, I'm having a tough time getting my head around what is going on when I Instantiate.
If I do the following:
public GameObject prefab;
public GameObject prefabClone;
void Start()
{
prefabClone = Instantiate(prefab, transform.position, transform.rotation);
Debug.Log(prefabClone);
}
I get the output: prefab(Clone) (UnityEngine.GameObject)
Visual studio also gives me all of the functionality of a GameObject without explicitly casting it like this:
prefabClone = Instantiate(prefab, transform.position, transform.rotation) as GameObject;
Alternatively, if I load the object dynamically from the Resource folder like so:
public GameObject prefab;
public GameObject prefabClone;
void Start()
{
prefabClone = Instantiate(Resources.Load("awsomePrefab"));
Debug.Log(prefabClone);
}
Then the methods and properties are hidden from me (until I explicitly cast "as GameObject"). However, the output is the same regardless (of type GameObject).
In the first example when I am referencing a prefab of type GameObject, and storing it in the variable of type GameObject is the cast made implicitly now? What is the need to cast "as GameObject" if all of the GameObject methods and properties are exposed to me. Thanks for your help.
Answer by JedBeryll · Feb 20, 2019 at 03:57 PM
Resources.Load("awsomePrefab")
will load a UnityEngine.Object because it doesn't know the type of the item you want to load. You could use the same method to load a texture or anything but it doesn't know what you want unless you specify it like Resources.Load<GameObject>("awsomePrefab")
.
Same goes for Instantiate which could also take the type parameter like Instantiate(prefab), but you don't have to specify it because "prefab" is a GameObject so it is done implicitly. Now if you use Instantiate and Resources.Load together, you are loading an Object and Instantiating an Object.
Resources.Load("awsomePrefab"); //returns Object
Resources.Load<GameObject>("awsomePrefab"); //returns GameObject
Resources.Load("awsomePrefab") as GameObject; //returns GameObject
Instantiate(Resources.Load("awsomePrefab")); //returns Object
Instantiate(Resources.Load<GameObject>("awsomePrefab")); //returns GameObject
Instantiate(Resources.Load("awsomePrefab") as GameObject); //returns GameObject
Instantiate(Resources.Load("awsomePrefab")) as GameObject; //returns GameObject
So in the following example:
public GameObject prefab;
public GaneObject prefabClone;
void Start()
{
prefabClone = Instantiate(prefab, transform.position, transform.rotation);
}
Then I do not need to Instantiate "as GameObject" because this is implicitly done? That's what I expected. Thanks.
Hey sorry to bother you again. But I still can't find any documentation that says that the cast is implicit. Take this example from the documentation:
// Require the rocket to be a rigidbody.
// This way we the user can not assign a prefab without rigidbody
public Rigidbody rocket;
public float speed = 10f;
void FireRocket ()
{
Rigidbody rocketClone = (Rigidbody) Instantiate(rocket, transform.position, transform.rotation);
rocketClone.velocity = transform.forward * speed;
// You can also access other components / scripts of the clone
rocketClone.GetComponent<$$anonymous$$yRocketScript>().DoSomething();
}
What is the point of the explicit cast here? Since it should be implicitly set as a Rigidbody. All of the documentation and other threads say that Instantiating returns an Object, and you must cast the Object returned to GameObject/Rigidbody/Transform. The official tutorial (2:18) is also guilty of this.