PhotonPlayer display other players name

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.

PhotonPlayer display other players name

NomadicWarrior
2018-03-04 13:04:18

Hi, how can I display other player's name? I have 4 players in room. I have already displayed name of local player and 1 other player. I have no idea how to display other 2 player's name.

private void UpdatePlayerTexts()  
	{  
		PhotonPlayer remote = PhotonNetwork.player.GetNext();  
	  
		PhotonPlayer local = PhotonNetwork.player;

		if (remote != null)  
		{  
			this.playerNames[1].text = remote.NickName + "        ";  
		}

		else  
		{

			//TimerFillImage.anchorMax = new Vector2(0f,1f);  
			//this.TimeText.text = "";  
			//this.playerNames[1].text = "waiting for another player        00";  
		}

		if (local != null)  
		{  
			// should be this format: "YOU   00"  
			//this.playerNames[0].text = "YOU   " + local.GetScore().ToString("D2");  
			this.playerNames[0].text = "YOU";  
		}  
	}

I have tried to add
PhotonPlayer remote2 = PhotonNetwork.player.GetNext();
if(remote2 != null)
{
this.playerNames[2].text = remote2.NickName +" ";
}

But all other texts displayer only 1 other players name.
Thank you

Comments

[Deleted User]
2018-03-06 09:45:15

Hi @NomadicWarrior,

you can set the player's nick name by using PhotonNetwork.player.NickName = "NickName";. This way the name gets automatically synchronized across the network, too. In order to access a player's nick name you can use the same call: string nickName = PhotonNetwork.player.NickName;.

If you want to access the nick name from an object instantiated by the player, you can use the object's PhotonView component to get the name: string nickName = GetComponent().owner.NickName;.

If you want to have a list with all player names, you can iterate through the PlayerList for example with a for-each loop like shown below:

foreach (PhotonPlayer p in PhotonNetwork.playerList)  
{  
    string nickName = p.NickName;  
}

Please note, that the PlayerList is only available, when the client is inside a room.

NomadicWarrior
2018-03-06 14:06:33

hi @Christian_Simon , thank you for answer
I have 4 players in room and 4 text ui
I have to set each players name to their uniq text. As RockPaperScissors. I don't need list of players.
if is local set my name to Text[0].text = local player's name;
Text[1].text = remote1 players's name;
Text[2].text = remote2 player's name;
Text[3].text = remote3 players name;

NomadicWarrior
2018-03-07 20:46:01

Anyone please, I still can't see other players name

public string[] playerMass = new string[4];  
public string pName1;  
public string pName2;  
public string pName3;  
public string pName4;

public string _yourName;  
                pName1 = playerMass [0];  
		pName2 = playerMass [1];  
		pName3 = playerMass [2];  
		pName4 = playerMass [3];

_yourName = PhotonNetwork.playerName;  
		//PhotonPlayer _players = PhotonNetwork.player.GetNext ();  
		//playerMass [PhotonNetwork.playerList.Length] = PhotonNetwork.player.NickName;

		foreach (PhotonPlayer p in PhotonNetwork.playerList)  
		{  
			playerMass [PhotonNetwork.playerList.Length] = p.NickName;  
		}

NomadicWarrior
2018-03-12 13:59:36

hi @Christian_Simon , thank you for answer
I have total 4 players in room. And I have 4 UI Text gameobject.
Text 1 - is mine (local) "You"
Text2, Text3, Text4 - for other players;
Example:
Text1 = You
Text2 = NomadicWarrior
Text3 = Christian_Simon
Text4 = Bob.

Romin
2018-06-09 05:27:42

IF you want to set data in a lobby correct way to do this is

public override void OnJoinedRoom()  
{  
     photonView.RPC("SetLobbyPlayersData",PhotonTargets.all);  
}

[PunRPC]
public void SetLobbyPlayersData()
{
for (int index = 0; index < PhotonNetwork.playerList.Length; index++)
{
text_playerNames[index].text = "";

    }

    int i = 0;

    for (int index = 0; index < PhotonNetwork.playerList.Length; index++)  
    {

        try  
        {  
            text_playerNames[index].text = PhotonPlayer.Find(i + 1).NickName;  
            playersInfo[index].SetActive(true);  
        }  
        catch (Exception e)  
        {  
            index--;  
        }

        i++;

    }        
}

This way even if anyone leaves lobby and somone else joins later it will be handled

deadlysnix
2018-07-05 09:14:23

I attached the following script on the input field:

public class NameFestlegen : Photon.MonoBehaviour  
{  
    public static string Name;  
    private GameObject Dings;

    public void Run()  
    {  
        print(PhotonNetwork.player.NickName);  
        Name = gameObject.GetComponent().text;  
        PhotonNetwork.player.NickName = Name;  
        print(PhotonNetwork.player.NickName);  
    }  
}

And this script on the players avatar:

  
public class ShowInfoOfPlayer : Photon.MonoBehaviour  
{  
    private GameObject textGo;  
    private TextMesh tm;  
    public bool DisableOnOwnObjects;

    void Start()  
    {  
        if (tm == null) //wenn textmesh  
        {  
            textGo = gameObject.GetComponentInChildren().gameObject;  
            tm = textGo.GetComponent();  
        }  
    }

    void Update()  
    {  
        bool showInfo = !this.DisableOnOwnObjects || this.photonView.isMine;  
        if (textGo != null)  
        {  
            textGo.SetActive(showInfo);  
        }  
        if (!showInfo)  
        {  
            return;  
        }  
        PhotonPlayer owner = this.photonView.owner;  
        if (owner != null)  
        {  
            tm.text = (string.IsNullOrEmpty(owner.NickName)) ? "player" + owner.ID : owner.NickName;  
        }  
        else if (this.photonView.isSceneView)  
        {  
            tm.text = "scn";  
        }  
        else  
        {  
            tm.text = "n/a";  
        }  
    }  

It works find but I only tried it with two players. Maybe it works with more players too.

Luntay
2022-06-22 06:29:48

NomadicWarrior 2018-03-07T20:46:01+00:00
Anyone please, I still can't see other players name

public string[] playerMass = new string[4];
  
public string pName1;
  
public string pName2;
  
public string pName3;
  
public string pName4;
  

  
public string _yourName;
  
               pName1 = playerMass [0];
  
  	pName2 = playerMass [1];
  
  	pName3 = playerMass [2];
  
  	pName4 = playerMass [3];
  

  
_yourName = PhotonNetwork.playerName;
  
  	//PhotonPlayer _players = PhotonNetwork.player.GetNext ();
  
  	//playerMass [PhotonNetwork.playerList.Length] = PhotonNetwork.player.NickName;
  

  
  	foreach (PhotonPlayer p in PhotonNetwork.playerList)
  
  	{
  
  		playerMass [PhotonNetwork.playerList.Length] = p.NickName;
  
  	}
with this bad boi's just have to put these underneath so they are set after thet have been assigned=D    
pName1 = playerMass [0];    
pName2 = playerMass [1];    
pName3 = playerMass [2];    
pName4 = playerMass [3];    
Back to top