- Home /
Displaying the players through GUI
Hey! I have a PlayerList and am trying to create a scoreboard. At the moment I am having trouble displaying the player's in order (I am trying to sort them by score). First, I sorted them using "Linq" and then I tried to display them in GUI in that order. Unfortunately this hasn't worked so far, all player's are on the same place and it does not even update when a player's score increases. Anybody know how I could make this work? Thank you in advance!
void OnGUI ()
{
int Row = 1;
int rowHeight = 30;
foreach(Player pl in NetworkManager.Instance.PlayerList.OrderByDescending(x => x.Score))
{
for(int i = 0; i < NetworkManager.Instance.PlayerList.Count(); i++)
{
//Now if you do something like
Row = i + 1;
//because 1st place will be associated with 0
//Each item in the list is associated with the index 'i'
}
GUI.Label(new Rect(Screen.width * (2.6f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.PlayerName);
GUI.Label(new Rect(Screen.width * (4.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Score.ToString());
GUI.Label(new Rect(Screen.width * (5.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Kills.ToString());
GUI.Label(new Rect(Screen.width * (6.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Assists.ToString());
GUI.Label(new Rect(Screen.width * (7.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Deaths.ToString());
}
Wow I am such an idiot -.- Once again have I made something super simple and made it super complicated (in my head). Ironically this was the solution I had before I used Linq to display the list, I just did not think this would sort the score board properly so I changed it....Thank you very much! If you put this as an answer, I will mark it as correct.
Answer by Lo0NuhtiK · Apr 05, 2014 at 10:02 PM
change line 3 to = 0 ;
move bracket at line 20 to line 21
at new line 20 -> Row++ ;
delete entire for loop from line 8-thru-14
Final code (if someone wants to do something like this):
using System.Linq;
void OnGUI()
{
foreach(Player pl in Network$$anonymous$$anager.Instance.PlayerList.OrderByDescending(x => x.Score))
{
GUI.Label(new Rect(Screen.width * (2.6f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.PlayerName);
GUI.Label(new Rect(Screen.width * (4.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Score.ToString());
GUI.Label(new Rect(Screen.width * (5.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.$$anonymous$$ills.ToString());
GUI.Label(new Rect(Screen.width * (6.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Assists.ToString());
GUI.Label(new Rect(Screen.width * (7.0f/10f),Screen.height * (1.7f/6.3f) + Row * rowHeight, Screen.width * (0.3f/2.3f), Screen.height * (0.3f/6.3f)), pl.Deaths.ToString());
Row ++;
}
}
Thank you :)
Your answer
Follow this Question
Related Questions
Static class member for offset 1 Answer
A node in a childnode? 1 Answer
C# List and GUI 3 Answers
gui list problem 2 Answers