I did this but it prints one name multiple times. How do I make sure it prints one name only one at a time:
foreach(Player player in PhotonNetwork.PlayerList)
{
if(race_entered)
{
for(int i = 0; i <= PhotonNetwork.PlayerList.Length; i )
{
player_name[i].text = player.NickName;
}
}
}
CodePudding user response:
You are currently iterating exponentially. For every player you again iterate all players and overwrite all UI texts with the current player.
What you want is iterating only once
if(race_entered)
{
// cache since property access might be expensive
var players = PhotonNetwork.PlayerList;
// Note btw for iterating collections you always want an index
// "< Length" instead of "<= Length"
for(int i = 0; i < players.Length; i )
{
var player = players[i];
player_name[i].text = player.NickName;
}
}