I have a single script attached to a gamemanager object in my game that has two sections: one as a GameManager that holds data between scenes and one as a Serializable section which converts the data from the GameManager to enable the Save function to save it.
If I play the game through from scene 1 - scene 3 and then save, the variables are all successfully read by my Save function (in a separate script). If, however, I load a previous game, I run into problems. The data is successfully loaded into the game and I can play it (ie. the GameManager.control.xxxx variables successfully use the loaded data), but when I go to save again, I can’t because the game cannot find the Serializable section (PlayerData.current.xxxx). ie. Unity says object reference is not set to an object for the following line of the Save function:
Debug.Log ("Saved Data: " + PlayerData.current.healthpotion);
This is despite the fact it’s already accessed it to make the original save file. Is there any reason why a game wouldn’t be able to see another section of the same script?
Main script
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static GameManager control;
//recording names
public string username;
//setting up potion score
public int hlthpotion;
public int mnpotion;
....
[System.Serializable]
public class PlayerData {
public static PlayerData current;
public string UserName;
public int healthpotion;
public int manapotion;
public bool redWizard;
public bool purpleWizard;
public bool blueWizard;
public PlayerData () {
UserName = GameManager.control.username;
redWizard = GameManager.control.redwizard;
blueWizard = GameManager.control.bluewizard;
purpleWizard = GameManager.control.purplewizard;
healthpotion = GameManager.control.hlthpotion;
manapotion = GameManager.control.mnpotion;
}
}
Loading/Saving script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveLoad {
public static List<PlayerData> savedGames = new List<PlayerData> ();
public static void Save () {
Debug.Log ("Original Data: " + GameManager.control.hlthpotion);
Debug.Log ("Saved Data: " + PlayerData.current.healthpotion);
PlayerData.current.healthpotion = GameManager.control.hlthpotion;
PlayerData.current.manapotion = GameManager.control.mnpotion;
Debug.Log ("Game To Be Saved");
Debug.Log ("Original Data" + GameManager.control.hlthpotion);
Debug.Log ("Saved Data" + PlayerData.current.healthpotion);
SaveLoad.savedGames.Add (PlayerData.current);
Debug.Log ("Game Added");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
bf.Serialize (file, SaveLoad.savedGames);
file.Close ();
Debug.Log ("Game Saved " + PlayerData.current.UserName);
}
public static void Load() {
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
Debug.Log ("Save File exists");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
// SaveLoad.savedGames = (List<PlayerData>)bf.Deserialize (file);
List<PlayerData> playerDataList = (List<PlayerData>)bf.Deserialize (file);
file.Close ();
Debug.Log ("Save File Closed");
foreach (PlayerData Pd in playerDataList) {
GameManager.control.username = Pd.UserName;
Debug.Log ("Username matched");
GameManager.control.redwizard = Pd.redWizard;
Debug.Log ("Red matched");
GameManager.control.bluewizard = Pd.blueWizard;
Debug.Log ("Blue matched");
GameManager.control.purplewizard = Pd.purpleWizard;
Debug.Log ("Purple matched");
GameManager.control.hlthpotion = Pd.healthpotion;
Debug.Log ("Health Potion = " + GameManager.control.hlthpotion);
GameManager.control.mnpotion = Pd.manapotion;
// Debug.Log ("Mana Potion: " + PlayerData.current.manapotion);
}
}
}
}