- Home /
Photon Network get players in a room
Hi,
I'm using the Photon Network, and I was wondering how to get the players that are currently in the room? Like their player name, etc.
Thanks.
Answer by Caio_Lib · Mar 20, 2014 at 05:45 AM
Hi,
You can use:
PhotonNetwork.playerList - List of ALL player in the room.
Or you can use:
PhotonNetwork.playerName and PhotonNetwork.otherPlayers - Your player and list of others players.
How do we display the names as a string though. I have a UI.Canvas with a UI.Text child but I can't get it to display the name of the players. I have one UI.Text and it displays my name with
inRoomPlayer1.text = PhotonNetwork.playerName;
Now I want to display the names of the other 3 players in the room. I've tried a bunch of lines, such as
inRoomPlayer2.text = PhotonNetwork.playerList[1].name;
I've added things to the above code, I've created new variables to store the PhotonPlayer, no matter what I've tried so far though I haven't been able to get the playerName.
Hi superluigi, You could try:
var playerList = new StringBuilder();
foreach (var player in PhotonNetwork.otherPlayers)
{
playerList.Append(player.name + "\n");
}
m_Text.text = playerList.ToString();
Hope it helps!
Hello! I want to know that after you get the list of all player in the room, how can I access other script in a player object.
Like, each player has to have a photonView.cs attached, right? How can I then use it to access/refer other script which attach to the same object using the list I get from PhotonNetwork.playerList?
Hi loliconest, Using PhotonNetwork.otherPlayers you can identify players by ID, you can call RPC method passing ID as parameter and check if ID is equal to the current object.
Example: $$anonymous$$arco Polo Tutorial
Documentation: RPCs And RaiseEvent
Answer by saadali211 · Mar 02, 2020 at 08:15 AM
here is how you list players name in current room in Photon Unity Networking PUN2
public void AddAllActivePlayers()
{
Dictionary<int, Photon.Realtime.Player> pList = Photon.Pun.PhotonNetwork.CurrentRoom.Players;
foreach (KeyValuePair<int, Photon.Realtime.Player> p in pList)
{
print(p.Value.NickName );
}
}