- Home /
Player sorting scoreboard
Hi!
I have made a simple scoreboard using Gamer To Game Developer's tutorials but he used GUILayout and I changed mine to just "GUI". But now I have discovered that my scoreboard players aren't getting sorted of score. But if a player leaves/joins e.t.c the players do move. It is just that they aren't sorted. Any idea how I can fix this?
My code:
GUI.Box(new Rect(Screen.width/2 - 260, 50, 250, 600), "");
GUI.Label(new Rect(Screen.width/2 - 255, 55, 250, 30), "Humans", redHeaderStyle);
GUI.Label(new Rect(Screen.width/2 - 40, 55, 250, 30), redTeamScore.ToString(), redHeaderStyle);
for(int i = SortingList.Count - 1; i >= 0; i--){
if(SortingList[i].playerTeam == "red")
{
GUI.Box(new Rect(Screen.width/2 - 255, 80 + (i * 20), 300, 30), SortingList[i].playerName, redHeaderStyle);
GUI.Label(new Rect(Screen.width/2 - 40, 80 + (i * 20), 300, 30),SortingList[i].playerScore.ToString(), redHeaderStyle);
}
}
And please, don't post your answer in GUILayout.
Thanks
Answer by Chris_Dlala · Jun 08, 2014 at 01:06 PM
Hi, I'm assuming that SortingList
is a generic List due to the Count
property. Are you sorting this list at any point or are you simply adding/removing entries? If you aren'st currently, you'll probably want List.Sort. Here is an example sort using a Lambda expression:
SortedList.Sort((a, b) => a.playerScore.CompareTo(b.playerScore));
Another interesting article on sorting arrays. I hope that helps =)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Find index of item in list 1 Answer
I'm desperate with my scoreboard! (Array, Javascript) 1 Answer
How to sort a List by a class paramater 1 Answer
store/rearrange integers for a basic scoreboard with C# 2 Answers