Search Unity

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

Discussion in 'UNet' started by mightybob, Jun 24, 2017.

  1. mightybob

    mightybob

    Joined:
    Mar 23, 2014
    Posts:
    75
    I'm currently using this in order to give each player a unique ID.

    Code (CSharp):
    1. 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)

    Code (CSharp):
    1.  
    2. var spawn = NetworkManager.singleton.GetStartPosition();
    3. var newPlayer = ( GameObject) Instantiate(NetworkManager.singleton.playerPrefab, spawn.position, spawn.rotation );
    4.  
    5. NetworkServer.ReplacePlayerForConnection( connectionToClient, newPlayer, playerControllerId );
    6. 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?
     
  2. marcV2g

    marcV2g

    Joined:
    Jan 11, 2016
    Posts:
    115
    You could either track by connection id or just create your own player ids.
     
    mightybob likes this.
  3. mightybob

    mightybob

    Joined:
    Mar 23, 2014
    Posts:
    75
    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-

    Code (CSharp):
    1.  
    2.         if (isServer) {
    3.             uniqueName = "Player " + connectionToClient.connectionId.ToString ();
    4.         } else {
    5.             uniqueName = "Player " + connectionToServer.connectionId.ToString ();
    6.         }