Casting Object to GameObject

I have an object created using the Instantiate function and I want to cast it to a GameObject, but Unity won’t let me. Why not, since the object it’s creating should be a GameObject? And what do I do about it?

1 Like

Can you post the code you are using?

One of the instance variables is

public Transform avatar;

which is set to the correct prefab in the inspector. The Start method is OK with the line

Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

or

Object o = Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

but won’t allow me to cast the object to a GameObject.

Have you tried something like:-

GameObject go = (GameObject) Instantiate (avatar, new Vector3(0, 0, 0), Quaternion.identity);

…and found it doesn’t work?

2 Likes

I’ve tried that. It instantiates the object, but I get an InvalidCastException.

How about:-

GameObject go = (GameObject) Instantiate (avatar.gameObject, new Vector3(0, 0, 0), Quaternion.identity);

No, then it says “‘UnityEngine.Transform’ does not contain a definition for ‘GameObject’”.

Don’t know if you figured it out yourself, but I ran into the same problem today and thought I’d tell what the problem seems to be.
The object returned by Instantiate() is not a GameObject, but a Transform. So you can cast it to a transform and then get to the GameObject if you have to.

Transform t = Instantiate(prefab, pos, rot) as Transform;
GameObject go = t.gameObject;

Quite silly, because the documentation says otherwise. I guess they changed this some time ago and forgot to update the docs… :frowning:

4 Likes

The right solution. Thanks alot :slight_smile:
miao

you could also try:

GameObject go = GameObject.Instantiate( ... ) as GameObject;
3 Likes

Spree, I could kiss you.

1 Like

I would also like to kiss you.

Funny note: my friend and I are both working on this project via git. His doesn’t throw this error, and mine does. So, there’s something else going on here.

I’ll tell you more. I can successfuly do Instantiate(prefab) as GameObject; but in another class I get an error. What can I say…Unity!

Ahhhh… thank you for the solution :slight_smile:

Not true. Instantiate returns a UnityEngine.Object which is different from System.Object.
This should work as well:

GameObject t = Instantiate(prefab, pos, rot) as GameObject;
1 Like

Wow. Ton of weird misinformation in this thread.

Instantiate returns a UnityEngine.Object. The actual runtime type of this object is the same as the type of the object you pass in. So if you pass in a Transform you can cast the result to a Transform. If you pass in a GameObject you can cast the result to a GameObject. Same with a Component.

In any case, this is all pretty moot now, as there is a generic version of Instantiate available as of 5.0 (maybe earlier). The generic version eliminates the need to cast altogether.

The generic version does not work for the Instantiate version they’re talking about. It only works for Instantiate that takes 1 parameter. The Instantiates that takes 3 parameters can only take and return a UnityEngine.Object.

It appears from the docs you are right. That’s an odd design choice. Either way my comments about the type that is returned is still valid. There is no mystery about it, you get the same runtime type out as what you put in.

So this works

Transform myTransform;
...
Transform clone = (Transform) Instantiate (myTransform);

As dose this

GameObject myObject;
...
GameObject clone = (GameObject) Instantiate (myObject);

And the same principle for any thing else, rigidbody is a common one I use.

But this will not work, and will throw a runtime error.

Transform myTransform;
...
GameObject clone = (GameObject) Instantiate (myTransform);

The generic version of Instantiate is declared with T restricted to UnityEngine.Object. This means that you can call Instantiate on things that are not Components or GameObjects, which means that they don’t have an attached transform to feed the second and argument to.

The type argument can’t have any better restrictions, since the method needs to accept both GameObjects and Components, and both of those inherit directly from UnityEngine.Object.

It should probably accept those arugments anyways, and throw an error if you send in something like a ScriptableObject or whatever, but maybe there’s use cases where things that doesn’t have an attached transform is valid.

Thanks mate, this worked for me as well!