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! Your search result can be found below. Plus, we offer support via 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.

Pun 2 PlayerList

Avi3ator
2018-12-03 17:40:24

Im trying to create a playerlist so when a player joins it instantiates a button with his name on it, but all the tutorials are outdated, they dont work in PUN2, can someone help me?

Comments

[Deleted User]
2018-12-04 11:31:44

Hi @Avi3ator,

you would have to handle two different cases. The first one is a client who joins a room. This client can use the OnJoinedRoom callback to instantiate those buttons for himself and all other clients that are already in the room. To access them you can use and iterate through the PhotonNetwork.PlayerList.

If the client is already in the room and another client joins this room, you can use the OnPlayerEnteredRoom(Player newPlayer) callback. This one gets called whenever another client joins the room. You also would have to cover the case when a client leaves the room. This can be done by using the OnPlayerLeftRoom(Player otherPlayer) callback.

Avi3ator
2018-12-04 13:17:41

@Christian_Simon
Thank you for you help, but i cant get it to work as intended :/
The PlayerList only shows the local player name on both buttons?

[Deleted User]
2018-12-04 14:00:06

Where do you set the name of each player? Please make sure, that both players have unique names. You can also check this without creating any buttons. Simply use the following code snippet:

private void OnGUI()  
{  
    if (!PhotonNetwork.InRoom)  
    {  
        return;  
    }

    GUILayout.BeginVertical();

    foreach (Player p in PhotonNetwork.PlayerList)  
    {  
        GUILayout.Label(p.NickName);

        // You can also use this one instead of the above one  
        // GUILayout.Label(p.ToStringFull());  
    }

    GUILayout.EndVertical();  
}

This has to be in a script attached to a GameObject in the scene.

Avi3ator
2018-12-04 14:15:19

I set it in the start function in my player controller script

void Start() { if(PV.IsMine) { PhotonNetwork.NickName = PlayerInfo.PI.mySelectedCharacter; } }

[Deleted User]
2018-12-04 15:18:03

What is printed on the screen when you add the code snippet from above?
Please check, what the value of PlayerInfo.PI.mySelectedCharacter is on each client.

Avi3ator
2018-12-05 11:36:52

The Code snippet actually works but i cant get it work when i use the buttons

Avi3ator
2018-12-05 12:01:00

Here is a pic of the 2 players, as you can se in the background the old gui works but the new buttons dont

[Deleted User]
2018-12-06 09:14:51

It seems that the Start function of the Player Controller script is called after the OnPlayerEnteredRoom callback. In this case the player's nick name might be empty (as seen on the left picture). It is properly updated on the old GUI because it is repainted every frame.

If you have the possibility, you can try to set the player's nick name before joining the room. Another option would be, to update the button's text component from time to time. This would require saving a link between the player and "his" button somehow. Since the player's nick name is a Custom Player Property, you can also use the OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps) callback therefore. Again this would require, that you know which button belongs to which client.

Avi3ator
2018-12-06 09:37:55

Okay thank you will try to set the player nickname before joining, i think i know a way of doing that :)

Avi3ator
2018-12-06 09:52:27

Now this happends :open_mouth:

The host doesnt get the new button when another player joins, and the player 2 doesnt get the right names xd

[Deleted User]
2018-12-06 10:16:24

Players that are already in the room behave different compared to players who are joining the room. Please have a look at the following code snippet and try to insert it into your application's logic:

public class Name : MonoBehaviourPunCallbacks  
{  
    public override void OnJoinedRoom()  
    {  
        foreach (Player p in PhotonNetwork.PlayerList)  
        {  
            // Add a button for each player in the room.  
            // You can use p.NickName to access the player's nick name.  
        }  
    }

    public override void OnPlayerEnteredRoom(Player newPlayer)  
    {  
        // Add a button for the new player.  
        // You can access the new player's nick name by using newPlayer.NickName.  
    }

    public override void OnPlayerLeftRoom(Player otherPlayer)  
    {  
        // Remove a button that is related to the player who just left the room.  
    }  
}

Avi3ator
2018-12-14 20:10:25

Cant get it to work xd been trying for like 5 days now :/ think im going to give up

Back to top