Network.AllocateViewID problem

Hi everyone,

Im trying to instantiate my player character over the network using RPCs and Network.AllocateViewID. So Im trying to have the client allocate and send RPC for local instantiations on other connections.

The problem is: The character is getting instantiated but I cannot control him, his ViewID is 0. Im kind of really lost here if anyone could help me out. I appreciate it. :)

`void OnConnectedToServer()
{

if (!initialize_player) 
{
    initialize_player = true;   

}

}

void OnGUI()
{

    if (Network.isClient)
    {
        if (GUILayout.Button("SpawnPlayer")) 

        {

        //Network.Instantiate(playerPrefab, player_spawn_position, transform.rotation, 0);
        NetworkViewID viewID = Network.AllocateViewID();
        networkView.RPC("SpawnPlayer", RPCMode.AllBuffered, viewID, transform.position);
        }
    }

}

[RPC]
void SpawnPlayer(NetworkViewID viewID, Vector3 location)

{
GameObject clone;
clone = Instantiate(playerPrefab, location, Quaternion.identity) as GameObject;
NetworkView nView;
nView = clone.GetComponent();
nView.viewID = viewID;
}
`

Well i guess your playerPrefab is not a GameObject (the variable type). Instantiate clones the object you're handing in. If you provide a Transform then Instantiate will return a Transform (It will of course create the owner GameObject of the Transform).

That's why the "as" cast is not the best choice...

The “as” - cast will return a null reference if the given reference can’t be casted into the given type.

The "normal" c-style cast will give you a casting exception instead.

To solve your problem make sure your playerPrefab is of type GameObject. If you change the type you have to reassign your prefab.

public GameObject playerPrefab;

If you have it that way, i don't know what else could be wrong...

Hmm, maybe your prefab doesn't have a NetworkView?