Question by
Centrixed · Jul 31, 2016 at 01:36 AM ·
c#scripting problemnetworkinginstantiateunity5
Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter?
So I am trying to create a respawn function for my networked game in Unity3D. The dead character gets deleted, a new player is spawned and visible, but there's an issue. The error "Object reference is not set to an instance of an object" shows up whenever the ReplacePlayerForConnection function runs. I am absolutely stumped and have looked everywhere for answers. If anyone could help I would appreciate it.
Here is my code:
//Called when the player clicks the respawn button
[Client]
public void respawnClick()
{
CmdRespawnClick();
}
[Command]
public void CmdRespawnClick()
{
var spawn = NetworkManager.singleton.GetStartPosition();
if(charSelect.selectedChar == 1) //If character 1 is selected
{
GameObject player = Instantiate(Resources.Load("Characters/Player1", typeof(GameObject)), spawn.position, Quaternion.identity) as GameObject;
NetworkServer.DestroyPlayersForConnection(NetworkManager.singleton.client.connection);
NetworkServer.ReplacePlayerForConnection(connectionToClient, player, 0);
}
else if(charSelect.selectedChar == 2) //If character 2 is selected
{
GameObject player = Instantiate(Resources.Load("Characters/Player2", typeof(GameObject)), spawn.position, Quaternion.identity) as GameObject;
NetworkServer.DestroyPlayersForConnection(NetworkManager.singleton.client.connection);
NetworkServer.ReplacePlayerForConnection(connectionToClient, player, 0);
}
//Waits for a moment to de-activate the respawn button
StartCoroutine(WaitForRespawn());
}
Comment