Instantiate fails to convert into GameObject

I know this is a question that’s been asked before - but the posted solutions are not working. There is a prefab containing an empty gameobject with a network view and script attached to it - when called upon to instantiate, this happens:

chatObject = Instantiate(chatobj) as GameObject;

if ( chatObject == null )
{
	Debug.Log("Instantiate Failed");
	return;
}

If I just leave it as a unity object it’s fine - it’s only after being converted to a gameobject it becomes null.

You should try using explicit casting instead of the “as” keyword. If you use “as GameObject” your chatObject will turn to null if chatobj is not of type GameObject. With explicit casting, you’re telling the compiler to take it as a GameObject, even if it isn’t. So I guess your solution is:

change

chatObject = Instantiate(chatobj) as GameObject;

to

chatObject = (GameObject)Instantiate(chatobj);

hope it helps :wink: