- Home /
Save data not creating save file
I've done a save system before, and I didn't change anything about the system but it's not working. So here is my save script and it just creates a file and opens it when you want to load it. It keeps saying file not found and I'm getting a NullReferenceException
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using System.Runtime.CompilerServices;
public static class SaveScript
{
public static void SaveStats(PlayerControls controls)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/targetBlast.GameData";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(controls);
formatter.Serialize(stream, data);
stream.Close();
}
public static PlayerData LoadStats()
{
string path = Application.persistentDataPath + "/targetBlast.GameData";
if (File.Exists(path))
{
Debug.Log("File found" + path);
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData data = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return data;
}
else
{
Debug.Log("File not found" + path);
return null;
}
}
}
This is my main script where the variables that I'm saving are. I keep getting a NullReferenceException error saying Object reference not set to the instance of an object and I'm getting an error because of the variable "allPoints" for some reason.
public void SaveGame()
{
SaveScript.SaveStats(this);
}
public void LoadGame()
{
PlayerData data = SaveScript.LoadStats();
this.allPoints = data.allPoints;
}
Here is my last script where I store the player's data
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public int allPoints;
public bool levelUnlockedZero;
public bool levelUnlockedOne;
public bool levelUnlockedTwo;
public bool levelUnlockedThree;
public bool levelUnlockedFour;
public bool levelUnlockedFive;
public bool levelUnlockedSix;
public bool levelUnlockedSeven;
public bool hasAbilityZero;
public bool hasAbilityOne;
public bool hasAbilityTwo;
public bool hasAbilityThree;
public bool hasAbilityFour;
public bool hasAbilityFive;
public bool hasAbilitySix;
public bool hasAbilitySeven;
public PlayerData (PlayerControls controls)
{
allPoints = controls.allPoints;
levelUnlockedZero = controls.levelUnlocked[0];
levelUnlockedOne = controls.levelUnlocked[1];
levelUnlockedTwo = controls.levelUnlocked[2];
levelUnlockedThree = controls.levelUnlocked[3];
levelUnlockedFour = controls.levelUnlocked[4];
levelUnlockedFive = controls.levelUnlocked[5];
levelUnlockedSix = controls.levelUnlocked[6];
levelUnlockedSeven = controls.levelUnlocked[7];
hasAbilityZero = controls.hasAbility[0];
hasAbilityOne = controls.hasAbility[1];
hasAbilityTwo = controls.hasAbility[2];
hasAbilityThree = controls.hasAbility[3];
hasAbilityFour = controls.hasAbility[4];
hasAbilityFive = controls.hasAbility[5];
hasAbilitySix = controls.hasAbility[6];
hasAbilitySeven = controls.hasAbility[7];
}
}
Could this be a BUG? I'm having the exact same problem. File saves perfectly fine in editor but not at runtime. Odd thing is I'm creating the folder the file goes in at the same time, the folder gets created but not the file.
Nope I'm just an idiot, forgot to load Resources at runtime.
Answer by DevManuel · Mar 23, 2021 at 01:10 PM
I don't know the answer, but I've already a working SaveManager, maybe it helps you:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class SaveManager : MonoBehaviour
{
public bool load = true;
public bool save = false;
public SaveData activeSave;
void Awake()
{
Debug.Log(Application.persistentDataPath);
if(save){Save();}
if(load){Load();}
}
void Update()
{
if(Input.GetKeyDown(KeyCode.S))
{
Save();
}
if(Input.GetKeyDown(KeyCode.L))
{
Load();
}
}
public void Save()
{
//print("saving");
string dataPath = Application.persistentDataPath;
//print(dataPath);
XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
FileStream stream = new FileStream(dataPath + "/" + activeSave.saveName + ".xml", FileMode.Create);
//print(stream);
serializer.Serialize(stream, activeSave);
stream.Close();
//print("saved");
}
public void Load()
{
print("loading");
string dataPath = Application.persistentDataPath;
//print(dataPath);
if(System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".xml"))
{
print("exsits");
var serializer = new XmlSerializer(typeof(SaveData));
var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".xml", FileMode.Open);
//print(stream);
activeSave = serializer.Deserialize(stream) as SaveData;
//print(activeSave);
stream.Close();
print("loaded");
}
}
/*
//Load xml file
public void Load()
{
string dataPath = Application.persistentDataPath;
XmlSerializer serializer = new XmlSerializer(typeof(SaveData)); //Create serializer
FileStream stream = new FileStream(dataPath, FileMode.Open); //Load file at this path
activeSave = serializer.Deserialize(stream) as SaveData;
stream.Close();//Close the stream
}
*/
public void LoadData()
{
string dataPath = Application.persistentDataPath;
if (File.Exists(dataPath + "/" + activeSave.saveName + ".xml"))
{
//var serializer = new XmlSerializer(typeof(SaveData));
//var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Create);
//activeSave = serializer.Deserialize(stream) as SaveData;
string filePath2 = dataPath + "/" + activeSave.saveName + ".xml";
//string fileText = File.ReadAllText(filePath);
XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
using (StringReader reader = new StringReader(filePath2))
{
//return (SaveData)(serializer.Deserialize(reader)) as SaveData;
activeSave = (SaveData)(serializer.Deserialize(reader)) as SaveData;
print("loaded");
}
}
}
}
[System.Serializable]
public class SaveData
{
public string saveName;
public int age;
public int mass;
public int size;
public int fitLevel;
public bool tone;
public bool softAP;
public bool firstStart = true;
public List<float> ListGraph1;
public List<float> ListGraph2;
public List<float> ListGraph3;
}