🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Save/Load High Scores
NullReferenceException: Object reference not set to an instance of an object Timer.Update () (at Assets/Scripts/Timer.cs:50)
The code it is talking about looks like this:
if (gameTimer <= 0)
{
inGame.SetActive (false);
playArea.SetActive (false);
gameOver.SetActive (true);
finalScore.text = "Score: " + GameManager.score;
Manager.gameManager.Save();
}
It seems to be talking about the save function that looks like this:
public void Save ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/HihgScores.dat");
HighScores hScore = new HighScores ();
foreach(int score in hScore.highScores)
{
if (GameManager.score >= hScore.highScores[score])
{
hScore.highScores[score] = GameManager.score;
break;
}
}
bf.Serialize (file, hScore);
file.Close();
}
The class that this method is from has a public static version of itself (like the live training says) so I'm not sure why it can't find a reference to the game object since it's persisting in all scenes.
You're instantiating a new HighScores instance (hScore). Unless the constructor for HighScores populates itself with data (?) there will be nothing to dereference at the following line:
if (GameManager.score >= hScore.highScores[score])
because hScore.highScores (an array or list I'm assuming) hasn't been populated with any content. For all we know, hScore.highScores itself is still null.
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
how could you manage this for multiple users?
Off-the-cuff here, but a high level version would be if you want the scoreboard to reflect local players only, the game can just load up the external .dat file when you start the game (so the current high scores are in memory), and then save it back out on exit with any changes.
If you want it integrated to facebook and reflecting multiple remote users, that .dat file will need to live somewhere all the FB instances can access it, or be converted to a database table (with interface changes in the code as well as you'd no longer be loading in from a file stream).
If you want it integrated to facebook but still only want to track the local players' high scores, you can save some kind of file off into browser cache/cookies.
There are other solutions as well, my web-stack experience is much shorter than my desktop/enterprise knowledge.
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
I thought I knew what I was doing when I converted that test high score to an array but I guess not.
public void Save ()
{
Debug.Log ("Save() was called");
if (!File.Exists (Application.persistentDataPath + "/HihgScores.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/HihgScores.dat");
HighScores hScore = new HighScores ();
hScore.highScores = highScores;
bf.Serialize (file, hScore);
file.Close ();
}
else {
Load ();
}
}
public void Load ()
{
Debug.Log ("Load() was called");
if (File.Exists (Application.persistentDataPath + "/HihgScores.dat"))
{
//Debug.Log ("File Exists");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/HihgScores.dat", FileMode.Open);
HighScores hScore = (HighScores)bf.Deserialize (file);
System.Array.Copy(hScore.highScores, highScores, 10);
file.Close ();
//Debug.Log ("High Score: " + HighScoreManager.highScore);
}
//Debug.Log ("Exiting Load()");
}
[Serializable]
class HighScores
{
public int[] highScores = new int[10];
}
These changes now cause this error:
ArgumentException: Object type System.Int32 cannot be converted to target type: System.Int32[]
ArgumentException: Object type System.Int32 cannot be converted to target type: System.Int32[]
"An integer can't be converted to an array of integers"
Which line is throwing that error?
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
There should be more information in the Console tab/window, to include the full stack-trace of the error/output (when you select any entry in the Console list, i.e. one left click). Each entry in the trace should tell you which file it was in for that step, and which line the function call for that step happens at. Resize the console tab to see more (as the stacktrace content appears at the bottom of the window), or just highlight the whole mess and paste it into a notepad or something to read.
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
All this seems to be telling me is that there is a problem where save and load are called.