(UNET) Respawning the player causes the player to have a new netId

I’m currently using this in order to give each player a unique ID.

GetComponent<NetworkIdentity> ().netId;

Which works, but if I respawn the player by destroying and re-instantiating, they spawn with a new ID. (which I’m doing like so)

var spawn = NetworkManager.singleton.GetStartPosition();
var newPlayer = ( GameObject) Instantiate(NetworkManager.singleton.playerPrefab, spawn.position, spawn.rotation );

NetworkServer.ReplacePlayerForConnection( connectionToClient, newPlayer, playerControllerId );
NetworkServer.Destroy( gameObject );

So I’m really not sure how to fix this. I’m aware I could have a dead boolean and reset positions instead of actually respawning, but since I’m trying to have multiple classes you can spawn as, I need to re-instantiate every time.

Any ideas? Or could someone share how they did it in their own game?

You could either track by connection id or just create your own player ids.

1 Like

Thanks, didn’t know about connection id’s.

edit: It worked, by the way.

edit2: If anyone coming across this thru google is wondering exactly how you do this-

        if (isServer) {
            uniqueName = "Player " + connectionToClient.connectionId.ToString ();
        } else {
            uniqueName = "Player " + connectionToServer.connectionId.ToString ();
        }