I was searching the internet and came along a pretty efficient script to record high scores, when i copied and pasted all seemed find and was working, however i essentially needed for the scores to be added from a game scene rather than the actual high score scene. I don’t see why there would be a problem, the high score script is set to not be destroyed on awake. hopefully a fresh pair of eyes can spot my error but all i get back from debug.log(highscores[1].name) is null. When i debug “name” it returns the date which is correct, something seems to go wrong in my savinghighscore method
The class below is my highscore manager class which i was able to get off the internet
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// High score manager.
/// Local highScore manager for LeaderboardLength number of entries
///
/// this is a singleton class. to access these functions, use HighScoreManager._instance object.
/// eg: HighScoreManager._instance.SaveHighScore("meh",1232);
/// No need to attach this to any game object, thought it would create errors attaching.
/// </summary>
public class HighScoreManager : MonoBehaviour
{
private static HighScoreManager m_instance;
private const int LeaderboardLength = 5;
public static HighScoreManager _instance {
get {
if (m_instance == null) {
m_instance = new GameObject ("HighScoreManager").AddComponent<HighScoreManager> ();
}
return m_instance;
}
}
void Awake ()
{
if (m_instance == null) {
m_instance = this;
} else if (m_instance != this)
Destroy (gameObject);
DontDestroyOnLoad (gameObject);
}
public void SaveHighScore (string name, int score)
{
//possibly not saving
List<Scores> HighScores = new List<Scores> ();
int i = 1;
//im assuming that empty values are put in at this point
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
Debug.Log(HighScores.Count);
if (HighScores.Count == 0) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
} else {
for (i=1; i<=HighScores.Count && i<=LeaderboardLength; i++) {
if (score < HighScores [i - 1].score) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Insert (i - 1, _temp);
break;
}
if (i == HighScores.Count && i < LeaderboardLength) {
Scores _temp = new Scores ();
_temp.name = name;
_temp.score = score;
HighScores.Add (_temp);
break;
}
}
}
i = 1;
while (i<=LeaderboardLength && i<=HighScores.Count) {//5
PlayerPrefs.SetString ("HighScore" + i + "name", HighScores [i - 1].name);
PlayerPrefs.SetInt ("HighScore" + i + "score", HighScores [i - 1].score);
//Debug.Log(HighScores [i - 1].name+"this is score");//this line reached
i++;
}
}
public List<Scores> GetHighScore ()
{
List<Scores> HighScores = new List<Scores> ();
int i = 1;
while (i<=LeaderboardLength && PlayerPrefs.HasKey("HighScore"+i+"score")) {
Scores temp = new Scores ();
temp.score = PlayerPrefs.GetInt ("HighScore" + i + "score");
temp.name = PlayerPrefs.GetString ("HighScore" + i + "name");
HighScores.Add (temp);
i++;
}
return HighScores;
}
public void ClearLeaderBoard ()
{
//for(int i=0;i<HighScores.
List<Scores> HighScores = GetHighScore();
for(int i=1;i<=HighScores.Count;i++)
{
PlayerPrefs.DeleteKey("HighScore" + i + "name");
PlayerPrefs.DeleteKey("HighScore" + i + "score");
}
}
void OnApplicationQuit()
{
PlayerPrefs.Save();
}
}
public class Scores
{
public int score;
public string name;
}
This is from my tutorital class, ive check the string and the score and they are fine
public void AddScore(){
string time = System.DateTime.Now.ToString("h:mm tt")+" done in" +watch.timeTaken+ "seconds";
//score will be how many u get right
HighScoreManager._instance.SaveHighScore(time,organsCorr.Count);
highscore = HighScoreManager._instance.GetHighScore();
Debug.Log(highscore[0].name);
}
void Start () {
//curs = GameObject.Find("Cursor_prefab");//ataches cureser prefab to curs
watch=(StopWatch)FindObjectOfType(typeof(StopWatch));//the watch is essentially the main game controller
rand=SortedArray;//puts the sorted array into rand
randomGen(rand);//shuffles the rand array
organsCorr= new List<string>();//creates a new list of strings for the correct ansers
highscore = new List<Scores>();
highscore = HighScoreManager._instance.GetHighScore();
}
All i get back is null from the highscores list