How to Count Number of players in each team in the PunTeams script

Ive been trying to do this all day, i thought it would be simple enough but i cant seem to get the number of players in each team, see PunTeams.PlayersPerTeam is a dictionary and i have no idea how to translate that to show an integer for the number of players in each team, i did manage to use this...

foreach (var teamName in PunTeams.PlayersPerTeam.Keys)
{
GUILayout.Box("" + teamName.ToString());
List teamPlayers = PunTeams.PlayersPerTeam[teamName];
foreach (PhotonPlayer player in teamPlayers)
{
GUILayout.Label("> " + player.ToStringFull());
}
}

which will list the team mates as a GUI but what im trying to do is make the game start only when both teams have equal players and i need to physically count each person for that, also on an unrelated note though the GUI works it only updates when a new player joins the room, any ideas on how i can do this?


Comments

  • UPDATE, I fixed this by making my own manager which counts when a player enters joins and leaves a team from scratch, however im still having trouble making the gui update for all players. If anyone is having a similar issue and doesnt want to look into making your own manager check out the GetTeam() variable as it may prove useful
  • The team extensions are relatively simple and don't offer all that much comfort, sorry.

    If you can't count teams every time you need the values, then you can count them when teams change and cache the info.

    PUN has callbacks (described in PunBehaviour), to let you know when someone joined the game, leaves or changes a player property. Those three cases can change teams and their team count. It should be enough to update the count in those cases and use it for updating your UI.
    You can also do your own calls, when the teams change this way, to update your UI.

    I'm sorry, but I don't have code for this at the moment, so I hope the input helps you.
  • I get a null reference when trying to count players in a team

    PunTeams.PlayersPerTeam[PunTeams.Team.red].Count