Hello guys, I’m having some issues with the persistence save and load (from Mike Geig tutorial: Persistence: Saving and Loading Data - Unity Learn).
Everything works fine, saving and loading, but I’m working with arrays and whenever I increase the array’s length in the unity inspector (and there is already a save file) the load method reduces the length of the array to the previous one (the old save file length) making it impossible to increase its size after there is already a save file. I need this so I can update or change some features in later updates of my game without messing with the players save file.
Here is a bit of the code for better understanding:
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public class SaveScript : MonoBehaviour {
#region Singleton
public static SaveScript instance;
private void Awake()
{
if (!instance)
instance = this;
else
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
Load();
}
#endregion
[Header("[Easy Levels]")]
public bool[] unlockedEasyLevels;
public int[] easyStarsUnlocked;
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data._unlockedEasyLevels = unlockedEasyLevels;
data._easyStarsUnlocked = easyStarsUnlocked;
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
unlockedEasyLevels = data._unlockedEasyLevels;
easyStarsUnlocked = data._easyStarsUnlocked;
}
[Serializable]
class PlayerData
{
public bool[] _unlockedEasyLevels = SaveScript.instance.unlockedEasyLevels;
public int[] _easyStarsUnlocked = SaveScript.instance.easyStarsUnlocked;
}
So if anyone can help me or point out what I’m missing I would appreciate, really struggling on this problem =(
Thanks for your attention.