I’m playing around with the new networking system, and I can’t seem to figure out how to spawn a new player instance.
When a player reaches 0 health (or less), I execute the following code on the player GameObject:
NetworkServer.Destroy(gameObject);
// UPDATE: I think I should be using ReplacePlayerForConnection, so added this in code below
After this, I show the respawn GUI (which is just a button right now). When you click the button, it should create a new instance of the player prefab. I tried implementing it like this:
GameObject player = Instantiate<GameObject>(playerPrefab);
NetworkServer.Spawn(player);
NetworkServer.ReplacePlayerForConnection(NetworkManager.singleton.client.connection, player, NetworkManager.singleton.client.connection.playerControllers[0].playerControllerId);
But when I check the GameObject, its isLocalPlayer property isn’t true.
I’m not sure about the parameters of ReplacePlayerForConnection. It looks like this should be possible a lot easier than this…
All the UNET examples don’t destroy the GameObject, but just hide it. However, I want the player to be able to choose between different prefabs I created in the project (e.g. sniper, warrior, scout, whatever…), so that’s not an option.
Other stuff I tried adding:
NetworkClient.connection.playerControllers[0].gameObject = player; // get the local clients connection, get the first playerController (which is the only one) and assign the new player gameobject as its gameobject.
Which doesn’t work.
Anyone having any luck with this?