Having trouble getting a save file functioning correctly (New to Unity and coding)

Hi,

I am tinkering with this project:-

I have done quite a lot of polishing up with UI/environments and filebrowsing and stuff like that. I have slowly learnt a fair amount in the last 10 days of messing around with it.

I have one more hurdle to get over, and that is the save game functionality.

There is a DefaultSaveMemory script in the project (attached below)

I have a couple of issues.

The default scripts apparently save the game when the user stops playing (It appears to be triggered fine when I change scene for example). But I cannot find where in all of the scripts in the project, that the Save function is called.

Also, and this is the big one - Its not working. :frowning:

I get this error on Line 15 of the DefaultSaveMemory script in the console when I run it.

get_streamingAssetsPath can only be
called from the main thread.
Constructors and field initializers
will be executed from the loading
thread when loading a scene. Don’t use
this function in the constructor or
field initializers, instead move
initialization code to the Awake or
Start function.
UnityEngine.Application:get_streamingAssetsPath()
DefaultSaveMemory:Save(String, Byte)
(at
Assets/unityGB/Scripts/DefaultSaveMemory.cs:17)
UnityGB.Game:Finalize() (at
Assets/unityGB/Scripts/unityGB/ROM.cs:232)

I would appreciate any assistance in figuring this out. Again, I’ve only been scripting/unity-ing for 10 days, so please feel free to treat me like an idiot when explaining how I can fix this. :slight_smile:

Thanks

And now - the offending script:-

using UnityEngine;
using System;
using System.IO;

using UnityGB;

public class DefaultSaveMemory : ISaveMemory
{

    public void Save(string name, byte[] data)
    {
        if (data == null)
            return;

        string path = null;
#if UNITY_ANDROID
        path = Application.streamingAssetsPath;
#else
        Debug.Log("I don't know where to save data on this platform.");
#endif

        if (path != null)
        {
            try
            {
                File.WriteAllBytes(path + name + ".sav", data);
            }
            catch (System.Exception e)
            {
                Debug.Log("Couldn't save data file.");
                Debug.Log(e.Message);
            }
        }
    }

    public byte[] Load(string name)
    {
        string path = null;
#if UNITY_ANDROID
        path = Application.streamingAssetsPath;
#else
        Debug.Log("I don't know where to load data on this platform.");
#endif

        byte[] loadedData = null;

        if (path != null)
        {
            try
            {
                loadedData = File.ReadAllBytes(path + name + ".sav");
            }
            catch (System.Exception e)
            {
                Debug.Log("Couldn't load data file.");
                Debug.Log(e.Message);
            }
        }
        return loadedData;
    }
}

Hi,

The persistent data path was the answer. It seemed to solve my save game issue.

Thanks :slight_smile: