- Home /
Get score from list?
hey gang trying to display scores from my list, should be a simple thing haven't been able to get it to work was wondering how i can get it. however its not displaying anything in my text. :(
should be something like FistPlace.text = scoresList[0].ToString();
public class ScoreTRacker : Photon.MonoBehaviour {
public List<int> scoresList;
public int Player1Score = 1000;
public int Player2Score= 4000;
public int Player3Score= 2000;
public int Player4Score= 3000;
public Text FistPlace;
public Text SecondPlace;
public Text FourthPlace;
public Text ThirdPlace;
void EndGame(){
scores.Add(Player1Score);
scoresList.Add(Player2Score);
scoresList.Add(Player3Score);
scoresList.Add(Player4Score);
scoresList.sort(); // put in order
}
void Update(){
int one = 0;
int two = 1;
int three = 2;
int four = 3;
// display in order from first in list to last....
FistPlace.text = scoresList[one].ToString();
SecondPlace.text = scoresList[two].ToString();
ThirdPlace.text = scoresList[three.ToString();
FourthPlace.text = scoresList[four].ToString();
}
Comment
Answer by unit_nick · Oct 17, 2017 at 04:34 AM
I realize it's not exactly what you have asked for but thought I would show custom list sorting in case it's useful
public List<Player> players;
public class Player
{
public int id
{
get { return _id; }
}
public int score
{
get { return _score; }
set { _score = value; }
}
private int _id;
private int _score;
public Player(int id)
{
_id = id;
_score = 0;
}
}
private void Start()
{
players = new List<Player>();
for (int i = 0; i < 4; i++) players.Add(new Player(i));
players[0].score = 3;
players[1].score = 2;
players[2].score = 8;
players[3].score = 1;
}
private void Update()
{
players.Sort(delegate (Player a, Player b)
{
if (a.score > b.score) return -1;
else return 1;
});
foreach(Player player in players)
{
Debug.Log(String.Format("Player {0} score = {1}", player.id, player.score));
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612141222im_/https://answers.unity.com/themes/thub/images/avi.jpg)