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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

display nicknames of multiple clients

Discussion in 'Multiplayer' started by alex5000, Dec 16, 2015.

  1. alex5000

    alex5000

    Joined:
    Jun 14, 2014
    Posts:
    1
    Hello! What's wrong? I want to display nicknames of multiple clients, but it's not working.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerData : NetworkBehaviour
    6. {
    7.     private NetworkManagerCustom NMC;
    8.     public Text nicknameText;
    9.     [SyncVar]
    10.     private string nickname = "";
    11.  
    12.     void Start()
    13.     {
    14.         if (isLocalPlayer)
    15.         {
    16.             NMC = GameObject.Find("NetworkManager").GetComponent<NetworkManagerCustom>();
    17.             nickname = NMC.nickname;
    18.             CmdOutputNickname();
    19.         }
    20.     }
    21.  
    22.     [Command]
    23.     void CmdOutputNickname()
    24.     {
    25.         nicknameText.text = nickname;
    26.     }
    27. }
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    use OnStartLocalPlayer() instead of Start()
     
  3. ObliviousHarmony

    ObliviousHarmony

    Joined:
    Jul 3, 2014
    Posts:
    79
    In addition to this, you should keep in mind that SyncVar members cannot be set on the client. Your command should be setting the SyncVar on the server, and then a hook should be updating the status on the client. Something like this, essentially:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerData : NetworkBehaviour
    7. {
    8.     private NetworkManagerCustom NMC;
    9.     public Text nicknameText;
    10.    
    11.     [SyncVar(hook="OnSyncVar_Nickname")]
    12.     private string nickname = "";
    13.  
    14.     void OnStartLocalPlayer()
    15.     {
    16.         NMC = GameObject.Find("NetworkManager").GetComponent<NetworkManagerCustom>();
    17.         CmdOutputNickname(NMC.nickname);
    18.     }
    19.  
    20.     [Command]
    21.     void CmdOutputNickname(string szNickname)
    22.     {
    23.         nickname = szNickname;
    24.         nicknameText.text = nickname;
    25.     }
    26.    
    27.     private void OnSyncVar_Nickname(string szNickname)
    28.     {
    29.         nickname = szNickname;
    30.        
    31.         if (isLocalPlayer)
    32.             nicknameText.text = nickname;
    33.     }
    34.    
    35. }
    36.  
     
    jathrek likes this.