I’ve built a saving/persistent data method that I think is successfully saving to my hard-drive(based on the console logs coming through), but I can’t seem to get my game to actually use the data after loading. The error logs indicate it’s running through the loading function, but it doesn’t actually use the data in the continued game (ie. my PlayerData.current remains empty after loading save game data). Any ideas what I’ve done wrong/can do to fix it?
My Save/Load functions:
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 () {
SaveLoad.savedGames.Add (PlayerData.current);
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);
file.Close();
}
}
}
My Serializable
[System.Serializable]
public class PlayerData {
public static PlayerData current;
public string UserName;
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;
Calling the load function
public void GameLoad () {
SaveLoad.Load ();
Debug.Log ("Save Game Loaded Step 1");
Application.LoadLevel ("Catch Potions");
}
}
}
}