Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Borzi · Apr 05, 2014 at 06:01 PM · guilistclasslinq

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());
             }    
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Borzi · Apr 06, 2014 at 06:29 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Borzi · Apr 06, 2014 at 10:39 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Edit each Parameter Class on Function 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges