Hi,
We have been developing a game with a leaderboard. When we use the laptop, the leaderboard works well. However, when we play the game on the android, the leaderboard displays a bunch of duplicates:
e.g. B 0.587
B 0.587
C 0.458
C 0.458
A 0.400
Where A,B,C are the names of the players and the float values are the score.
Below is the code:
public static class LeaderBoard {
public const int EntryCount = 5;
public struct ScoreEntry{
public string name;
public float score;
public ScoreEntry(string name, float score) {
this.name = name;
this.score = score;
}
}
public static List<ScoreEntry> getPlayers;
private const string PlayerPrefsBaseKey = "leaderboard";
public static List<ScoreEntry> initialise() {
getPlayers = new List<ScoreEntry> ();
Debug.Log (getPlayers.Count);
return getPlayers;
}
public static void createScore(string name, float score){
ScoreEntry player = new ScoreEntry(name, score);
getPlayers.Add(player);
}
public static ScoreEntry GetEntry(int index) {
return getPlayers[index];
}
public static void sortScores(){
getPlayers.Sort((a, b) => b.score.CompareTo(a.score));
}
public static void LoadBoard(){
for(int i=0; i<EntryCount; i++){
ScoreEntry entry;
entry.name = PlayerPrefs.GetString(PlayerPrefsBaseKey + "[" + i + "].name", "");
entry.score = PlayerPrefs.GetFloat(PlayerPrefsBaseKey + "[" + i + "].score", 0f);
getPlayers.Add(entry);
}
}
public static void saveScore(){
//save first score
PlayerPrefs.SetString(PlayerPrefsBaseKey + "[0].name",getPlayers[0].name);
PlayerPrefs.SetFloat(PlayerPrefsBaseKey + "[0].score",getPlayers[0].score);
for (int i = 1; i < EntryCount; i++) {
if (getPlayers *.score > 0) {*
_ PlayerPrefs.SetString(PlayerPrefsBaseKey + “[” + i + “].name”,getPlayers*.name);_
_ PlayerPrefs.SetFloat(PlayerPrefsBaseKey + “[” + i + “].score”,getPlayers.score);
PlayerPrefs.Save();
}
}*_
* PlayerPrefs.Save();*
}
}