I wrote a serialization class, and a save data class. But for some reason I keep getting an error on save,
SerializationException: Type ‘UnityEngine.MonoBehaviour’ in Assembly ‘UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.
I don’t understand, my save data class is marked as serializable. Here’s my save data class,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SaveData : MonoBehaviour
{
private static SaveData _data;
public static SaveData data
{
get
{
if(_data == null)
{
_data = new SaveData();
}
return _data;
}
}
public double currentCoins;
public int[] producerLevels;
public int[] upgradeLevels;
public int nuts;
private void Awake()
{
Load();
}
private void OnApplicationQuit()
{
Serialization.Save("Save", data);
}
public void Load()
{
_data = (SaveData)Serialization.Load(Application.persistentDataPath + "/saves/Save.save");
}
}
The variables are being assigned in the scripts, also here’s my serialization script,
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class Serialization
{
public static bool Save(string saveName,object saveData)
{
BinaryFormatter formatter = GetBinaryFormatter();
if(!Directory.Exists(Application.persistentDataPath + "/saves"))
{
Directory.CreateDirectory(Application.persistentDataPath + "/saves");
}
string path = Application.persistentDataPath + "/saves" + saveName + ".save";
FileStream file = File.Create(path);
formatter.Serialize(file,saveData);
file.Close();
return false;
}
public static object Load(string path)
{
if (!File.Exists(path))
{
return null;
}
BinaryFormatter formatter = GetBinaryFormatter();
FileStream file = File.Open(path,FileMode.Open);
try
{
object save = formatter.Deserialize(file);
file.Close();
return save;
}
catch
{
Debug.LogErrorFormat("Failed to load file at {0}", path);
file.Close();
return null;
}
}
public static BinaryFormatter GetBinaryFormatter()
{
BinaryFormatter formatter = new BinaryFormatter();
return formatter;
}
}
I get the error upon saving, the saving function is currently being called on a tester button I have, which calls
Serialization.Save("Save", SaveData.data);
I get no errors on load, but of course, there’s probably no saved data to load because the save function doesn’t work.
For the record, I followed a tutorial for this, I don’t fully understand binary formatters lol, but the tutorial was missing some important info so…
Any help would be appreciated, thanks.
Edit: Oh f, I just removed the monobehavior from my SaveData class, no more error, however, saving doesn’t seem to work at all?