Save Not Saving

So, I am having an issue with saving/loading files. I have made a few classes that are supposed to handle all of the data with my players. The class I think that is being the problem is the saveload class which handles all saving and loading for important things. To “remember” all of the possible objects to load, it also stores an array list of strings to get all of the names of the objects. Here is the save code:

public void savePlayer(Player playerToSave)
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/players/" + playerToSave.getName() + ".plr");

        Player data = playerToSave;

        bf.Serialize(file, data);

        ArrayList saves = getPlayerFiles();
        saves.Add(playerToSave.getName());

        FileStream saveFile = File.Create(Application.persistentDataPath + "/players/saves.svs");

        bf.Serialize(saveFile, saves);

        file.Close();
    }

So far, I have not found any problems that jump out at me with that class. The other part that I was thinking that could act up is when I try and retrieve that ArrayList of player names. This is the code for that:

    public ArrayList getPlayerFiles()
    {
        if (File.Exists(Application.persistentDataPath + "/players/saves.svs"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/players/saves.svs", FileMode.Open);
            PlayerList data = (PlayerList)bf.Deserialize(file);
            file.Close();
            return data.saves;
        }
        return new ArrayList();
    }

I tried building the code, and testing that out just to see if it was something with the persistentDataPath and files not actually saving unless it was built, but there was no change. This actually brings up an error message that says NullReferenceException: Object reference not set to an instance of an object. This of course, happens whenever I try to access the arrayList file. I believe this error has something to do with properly saving or loading the serialized files, but I am not 100% sure. I am also still working on learning c# with unity. Any criticism of style, redundant code, or just any better programming skills would be greatly appreciated also.
Thanks!
-Zypher

I must admit, I haven’t used an ArrayList in ages. Generally, a generic List is better as it’s typesafe, just meaning you know what goes in it, so you’ll know what will come out of it.

I would ask what does data.saves actually return? Or is suppose to return?

Ive had a lot of luck using either Player.Prefs if its not a lot of info or scriptable objects for saving data locally. Stetting up online data these days WWWForm class and MySQL + PHP could be another option for you. Hey, just suggestions, I dont understand your script but works out for me most of the time, hope to help

Thanks for all of the advice. I was using ArrayLists because I have recently moved from java, and that is what I was used to using. I will start to move over to List though to see how that works to though. data.saves was supposed to return an ArrayList of strings that contained all of the names of the saved files. Sadly I looked into Player.Prefs, but I currently have about 500 lines of custom classes that are going into the player that need to be saved, so that wasn’t going to work for me. I will look into WWWForm and MySQL + PHP to see what those involve to see if those work better. I will post again when I have found a solution of some sort. Thanks for all of the advice!

I had the same issue after going thru the Unity live training on this serialization. After a few days I got this to work very well.

using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System;
using System.Collections.Generic;

public class SaveInfoNewer : MonoBehaviour
{
    public string data;


    public void LoadData()
    {
        string data = SaveInfoNewer.DeserializeData<string>("PleaseWork.save");
        Debug.Log("Loaded data is:" + data);
    }
    public void SaveData()
    {
        SaveInfoNewer.SerializeData(data, "PleaseWork.save");
        Debug.Log("Saved data");
    }
    public static void SerializeData<T>(T data, string path)
    {

        FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
        BinaryFormatter formatter = new BinaryFormatter();
        try
        {
            formatter.Serialize(fs, data);
            Debug.Log("Data written to " + path + " @ " + DateTime.Now.ToShortTimeString());
        }
        catch (SerializationException e)
        {
            Debug.LogError(e.Message);
        }
        finally
        {
            fs.Close();
        }
    }

    public static T DeserializeData<T>(string path)
    {

        T data = default(T);

        if (File.Exists(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                data = (T)formatter.Deserialize(fs);
                Debug.Log("Data read from " + path);
            }
            catch (SerializationException e)
            {
                Debug.LogError(e.Message);
            }
            finally
            {
                fs.Close();
            }
        }

        return data;
    }
}

That’s the best help I can give :frowning:

Honestly, mysql and php probably is going to be more than you need. You can easily handle this with json, and depending on where you want to save it, some encryption if you need to protect the data. If nothing else, using a free backend (playfab or app42 are ones I’m familiar with) would be good, but only if you expect your player to have an internet connection to save and load the data.

Ok. Hello all again. I just had a brain fart and put two and two together. There were actually no issues with my save/load code… The reason I got the error message “NullReferenceException: Object reference not set to an instance of an object”, is because I was calling these methods from outside of the PlayerManager class that they were in. I was only calling methods however so I completely forgot that I needed to make new PlayerManager object to actually call the method… I didn’t actually post the code calling the function because I never thought that would have been the issue. Sorry for not giving you guys enough information and thank you very much for all of your assistance. It was very helpful!