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

Each play has a custom name?

Discussion in 'Multiplayer' started by Athalansy, Sep 8, 2015.

  1. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Hello!
    I want to achieve this..

    So in the offline scene players choose what would they like their name to be, name gets saved to PlayerPrefs.

    Then when player connects, I want to pull that name from PlayerPrefs and set it to his Text which is child of Canvas which is child of the player.

    How would I go about doing that?
     
  2. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Send that name upon connectiong or server's request in network message or a command.
     
  3. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    How would I exactly do that?
    The only thing I managed to come up with so it kinda works (has tons of bugs when the player disconnects) is this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class Player_Name : NetworkBehaviour {
    7.     public string playerName;
    8.    
    9.     public override void OnStartLocalPlayer(){
    10.         playerName = PlayerPrefs.GetString("Name");
    11.         InvokeRepeating("InvokableFunction",5f,5f);
    12.     }
    13.    
    14.     [Command]
    15.     void CmdSendNameToServer(string nameToSend)
    16.     {
    17.         RpcSetPlayerName(nameToSend);
    18.     }
    19.    
    20.     [ClientRpc]
    21.     void RpcSetPlayerName(string name)
    22.     {
    23.         gameObject.transform.FindChild("Canvas").transform.FindChild ("Text").GetComponent<Text>().text = name;
    24.     }
    25.  
    26.     void InvokableFunction() {
    27.  
    28.         CmdSendNameToServer(playerName);
    29.  
    30.     }
    31.     void OnDestroy(){
    32.         CancelInvoke();
    33.     }
    34.    
    35.    
    36. }
    37.  
    I presume this isn't the right way to do it right?
     
  4. Athalansy

    Athalansy

    Joined:
    Oct 7, 2014
    Posts:
    17
    Managed to do it, just instead of invoking it I put it in fixed update!