Casting Object to GameObject fails

Dear Community,

have been searching for a simple solution for casting a simple Object to a GameObject in C#.
Probing several ways failed in Runtime Errors:

Object obj = Network.Instantiate(prefab, pos, rot, 0);
GameObject gameObj = obj as GameObject;

Result: No Runtime error, gameObj is null!

Other one:

GameObject obj = Network.Instantiate(prefab, pos, rot, 0) as GameObject;

Result: No Runtime error, but obj is null!
GameObject obj = (GameObject)Network.Instantiate(prefab, pos, rot, 0) as GameObject;

Result: Runtime error, InvalidCastException!
GameObject obj = (Object)Network.Instantiate(prefab, pos, rot, 0) as GameObject;

Result: No Runtime error, obj is null!

Please, how to cast from Object to GameObject.
GameObject is inherited from Object that must be a simple solution.

Thanks.

  • Is ‘prefab’ a GameObject?
  • Is the Object returned by Network.Instantiate null (without the cast, that is)?

I tend to prefer using ‘(GameObject)’ to cast rather than ’ as GameObject’. The latter returns null when something goes wrong, while the former throws an exception, which gives much more information on what’s going on.

Thanks for answering.

Yes, the prefab is a GameObject

No, the Object is alright.

Using “(GameObject)” casting return following Exception:

InvalidCastException: Cannot cast from source type to destination type.

I should also mention that this function where the casting happened is a RPC function.

I dont know why the hell you can not cast from “Object” to “GameObject” in a RPC function,
but I have found out that this happens only in RPC functions.

So the question is addressed to the Unity developers:

  • Why this behaviour occurs only in RPC?

And second:
Using this unneeded function, let you cast an “Object” to an “GameObject” in an RPC function.
Thats not clean, but the only solution I see right now:

public GameObject ObjectToGameObject(Object obj)
	{
		string originalName = obj.name;
		obj.name = GetInstanceID().ToString();
		GameObject gameObj = GameObject.Find(obj.name);
		gameObj.name = originalName;
		return gameObj;
	}

I’ve just discovered this nasty little behavior:

  • if you attach a gameobject instantiate script to a CAMERA, you will never get the instantiate reference back to your gameobject, which means
GameObject mynewobject = Instantiate(myprefab) as GameObject;

will correctly create ‘myprefab’ in the scene, but mynewobject will be always null.

When you transfer that very instruction to an empty gameobject, or somewhere else, the assignment works.
I don’t know if RPC calls behave identically, but I just to be sure I’d move the code from a camera, if it’s your case too.

That first line of code you wrote will work properly when attached to any gameobject. It’s irrelevant what other components that object might have attached, whether it’s a camera or anything else. So you have something else going on.

You can’t attach scripts to cameras, only to gameobjects. Cameras are components that are attached to gameobjects, and scripts are also components that are attached to gameobjects.

Also, careful about replying to posts that are over 2 years old, since it’s quite likely that the info is outdated and doesn’t apply to the current version of Unity.

–Eric