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];
}
}