Need help with doing nicknames, leaderboards and printing names on players

Options
Hey guys, i have tried to code nicknames and can't really think of how to do the rest :/ as it just seems really different.
Here is my code to start with:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Nickname : MonoBehaviour { public Text infoText; [Header("Nick Panel")] public InputField nickInput; private string nickname; // Use this for initialization public void NickPanel_OkBtn() { if (Input.GetKeyDown (KeyCode.Return)) { this.nickname = nickInput.text; PhotonNetwork.player.NickName = this.nickname; Application.LoadLevel ("Game"); } } }

Comments

  • Hi @o0heza0o,

    when using PhotonNetwork.player.NickName = this.nickname; you are actually setting the player's nick name. It gets automatically synchronized and is available for all clients afterwards. If you now want to display the player's name above his character's head for example, you can use the character's PhotonView component to access the player's nick name by using photonView.owner.NickName.

    For a leaderboard you can iterate through PhotonNetwork.playerList to access each client's data and think about a possibility to display necessary data.
  • o0heza0o
    Options
    so, PhotonNetwork.player.NickName = this.nickname; sets the players name and to display it i use photonView.owner.NickName then on the leaderboard use the PhotonNetwork.playerList
  • [Deleted User]
    edited October 2017
    Options
    From the code snippet I guess, that you have an Input Field in your game and a button to confirm (calling NickPanel_OkBtn) the player's nick name, am I right? In this case you don't need to check Input in NickPanel_OkBtn() function. The button itself should be enough in order to apply the new player's nick name.

    When you join a room, the player's chosen nick name gets synchronized with other clients. If you now instantiate your character using PhotonNetwork.Instantiate, you can either add a new script or attach the following to a script, which is already attached to the character's prefab.
    public void Awake()
    {
        // Think about how to display this player's name to others
        // You can for example use a TextMesh component and set its text to GetComponent<PhotonView>().owner.NickName;
    }
    You can also use the Start function for this, even OnEnable would work.

    For the scoreboard, please have a look at this discussion. There you have the basic idea on how to implement this.