Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to identify players

Discussion in 'Multiplayer' started by JamesElvin, Apr 26, 2014.

  1. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    How can you identify which player is what? For example, I would like to kick a certain player from the server or change the value of one of his variables but to do that I need to identify who it is. I have heard of something called network ID but I'm not sure what that is or what I do with it.

    (Unity answers is not working so I cannot search there).
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
  3. Omar47i

    Omar47i

    Joined:
    Mar 24, 2014
    Posts:
    3
    you can simply create a script called InitPlayer and assign it to each player prefab, and implement this in the script

    Code (csharp):
    1. public class InitPlayer : MonoBehaviour {
    2.  
    3.     public string id;
    4.  
    5.     void OnNetworkInstantiate(NetworkMessageInfo info)
    6.     {
    7.           if (networkView.isMine)                             // if i'm the owner of this player prefab
    8.           {
    9.                   id = Network.player.ToString();        // assign the id
    10.                                                                               // reflect the id assignment to all networked players
    11.                   networkView.RPC("SetPlayerInfo", RPCMode.OthersBuffered, id);
    12.           }
    13.      }
    14.  
    15.      [RPC]
    16.      void SetPlayerInfo(string _id)
    17.      {
    18.          id = _id;
    19.      }
    20. }

    now u can refer to each player with his public id field, u can also force each client to enter his name before connecting to server and sending his name to all connected players the same way
     
    Last edited: Apr 30, 2014
  4. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    Thank you.
     
  5. Omar47i

    Omar47i

    Joined:
    Mar 24, 2014
    Posts:
    3
    You are welcome