0

I’m currently creating a scoreboard for my multiplayer game. But first I need to list all players in an array. How can I insert each player into an array? On each device it only shows one player. For example, player1 is in pc[0] and player2 is in pc[1].

 NetworkManager networkManager = NetworkManager.singleton;
 pc = networkManager.client.connection.playerControllers;
 for (i = 0; i < pc.Count; i++)
 {
     if (pc[i].IsValid)

     showname.GetComponent<Text>().text = pc[0].gameObject.name;
     showscore.GetComponent<Text>().text = pc[0].gameObject.GetComponent<multscore>().score.ToString();

     showname2.GetComponent<Text>().text = pc[1].gameObject.name;
     showscore2.GetComponent<Text>().text = pc[1].gameObject.GetComponent<multscore>().score.ToString();
}
2
  • 1
    Welcome. I notice you don’t have a brace after your if statement. Is that intentional? Without that, the condition will only apply to the subsequent line, while the other remaining lines will be executed for every ’pc[]`, even if it’s invalid. I imagine that might cause problems here. Commented Mar 24, 2020 at 7:07
  • It should probably rather be if (!pc[i].IsValid) break; Then you wouldn't want to use fixed indices 0 and 1 but rather use i .. therefoe I would also store all the text references in arrays so you can map each player to its text using i
    – derHugo
    Commented Mar 24, 2020 at 8:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.