Top 10 Leaderboards Not Working
Hello, I'm currently trying to make a leader board for my game. I'm trying to write all the scores to a file and to check the scores against each other and replace them if needed. I have a read write file for my player info, such as level and such which works. But my leader board wont work. Am I doing something very obviously wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class LearboardTracker : MonoBehaviour {
public static LearboardTracker lt;
public int[] leaderboardScore = new int[10];
public int score;
private void Awake()
{
if (lt == null)
{
DontDestroyOnLoad(gameObject);
lt = this;
}
else if (lt != this)
{
Destroy(gameObject);
}
}
private void Start()
{
Load();
}
public void LeaderboardCheck()
{
if (score >= leaderboardScore[0])
{
for (int i = 0; i < leaderboardScore.Length - 1; i++)
{
leaderboardScore[i + 1] = leaderboardScore[i];
}
leaderboardScore[0] = score;
}
for (int a = 1; a < leaderboardScore.Length - 1; a++)
{
if (leaderboardScore[a - 1] >= score && score >= leaderboardScore[a])
{
for (int i = 0; i > leaderboardScore.Length - 2; i++)
{
leaderboardScore[i + (a + 1)] = leaderboardScore[i + a];
}
leaderboardScore[a] = score;
}
}
if (leaderboardScore[8] >= score && score >= leaderboardScore[9])
{
leaderboardScore[9] = score;
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerScores.txt");
Scores data = new Scores();
for (int i = 0; i < leaderboardScore.Length; i++)
{
data.leaderboardScore[i] = leaderboardScore[i];
}
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerScores.txt"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerScores.txt", FileMode.Open);
Scores data = (Scores)bf.Deserialize(file);
file.Close();
for (int i = 0; i < leaderboardScore.Length; i++)
{
leaderboardScore[0] = data.leaderboardScore[0];
}
}
}
public void SetScore(int score2)
{
score = score2;
}
}
[Serializable]
class Scores
{
public int[] leaderboardScore = new int[10];
}
Through debugging I know the score is being passed through, LeaderboardCheck, Save and Load is being called successfully. However, it applies this score to all elements of leaderboardScore and up on exit, it only saves element 0. Any help or guidance would be greatly appreciated.