Problem with Persistence Save and Load Script.

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.

If you make sure the size of the loaded data can fit into your array, you could copy the contents over. The “new” unused portion would retain the default values you assigned.

1 Like

While you can use Array.Resize, you should really just use a List instead so you don’t have to worry about the size.

1 Like

I was thinking of Array.Copy… With a list, I’m pretty sure you’d have to use AddRange.
You couldn’t just assign the list from the loaded data to the class variable, as that would shorten it also if the intention was to have every level as an index*.

With either, copying them over one by one is of course also possible.

1 Like

Thank you for the response,
already tried the Array.Resize and it works like a charm, going to try the others and see which one is the best in this case.

Thanks again!