2016-09-13 20 views
0

我是一個新手,我陷入了這個問題。我可以讓球員的統計,得分和名字,但我不能讓記分牌正常工作。我已經工作了2天,試圖找出現在我問你們。Unity3D記分牌的排名和安置

我有前10名記分牌,但我無法進行安置。更高的分數應該有更高的位置。

這是我的代碼:

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(); 
     } 

    } 

我評論超過200行的代碼,因爲它沒有工作。但我希望你明白這一點。謝謝,如果你讓我的帖子。如果你能幫助我如何去做,我真的很讚賞它。謝謝。

回答

2

首先對玩家的分數進行排序。

using System.Linq; 

.... 

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

然後將循環中的得分分配給您的GUI。

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

我收到一個錯誤:ArgumentNullException:參數不能爲空。 參數名:源 –

+0

我想我應該讓更多的細節: –

+0

公共類PlayerSystem:NetworkedMonoBehavior { ... 公共靜態列表玩家= PlayerSystem.Players.OrderByDescending(P => p.Score).ToList (); ...} –