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

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

o0heza0o
2017-10-03 10:50:56

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

[Deleted User]
2017-10-05 08:12:49

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
2017-10-05 09:50:09

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]
2017-10-05 12:15:32

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().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.

Back to top