Atilla Gundogan
Atilla Gundogan

Reputation: 101

Ranking and Placement for Scoreboard in Unity3D

I am a newbie and I am stuck in this problem. I can the players stats, scores and names but I can't make the scoreboard work properly. I have worked 2 days trying to figure it out now I am asking you guys.

I have top 10 scoreboard but I cant make the placement. Higher score should have higher placement.

This is my code:

int PlayerCount = PlayerSystem.Players.Count;

    if(PlayerCount == 1)
    {
        Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
    }

    if (PlayerCount == 2)
    {
        if(PlayerSystem.Players[0].Score > PlayerSystem.Players[1].Score)
        {
            Score[0].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
            Score[1].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
        }

        else if(PlayerSystem.Players[1].Score > PlayerSystem.Players[0].Score)
        {
            Score[1].text = PlayerSystem.Players[0].Name + ": " + PlayerSystem.Players[0].Score.ToString();
            Score[0].text = PlayerSystem.Players[1].Name + ": " + PlayerSystem.Players[1].Score.ToString();
        }

    }

I commented over 200 lines of code because it didnt worked. But I hope you get the idea. Thank you if you readed my post. I really apreciate it if you help me how to do it. Thank you.

Upvotes: 0

Views: 793

Answers (1)

zwcloud
zwcloud

Reputation: 4889

First sort the player score.

using System.Linq;

....

List<Player> Players = PlayerSystem.Players.OrderByDescending(p=>p.Score).ToList();

Then assign the scores in a loop to your GUI.

for(int i=0; i<10; ++i)
{
    var player = PlayerSystem.Players[i];
    Score[i].text = player.Name + ": " + player.Score;
}

Upvotes: 2

Related Questions