Hello, I’m trying to make a save system in my deck building game, and I’m having trouble saving the player’s deck because it is a list of “Cards” which are scriptable objects. Here’s the save script:
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
public static void SaveDeck(Deck deck)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/deck.data";
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData data = new PlayerData(deck);
binaryFormatter.Serialize(stream, data);
stream.Close();
}
}
I receive the error SerializationException: Type ‘UnityEngine.ScriptableObject’ in Assembly ‘UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ is not marked as serializable. on the binaryFormatter.Serialize(stream, data); line. After some research, I tried making a separate SaveDeck class in order to try to implement a copy constructor, but I don’t understand copy constructors very well, so I couldn’t get it to work. Does anyone have a suggestion for how to fix the error, or can someone give details on how to implement a copy constructor? In case it’s helpful, here’s the PlayerData that this script is pulling from:
[System.Serializable]
public class PlayerData
{
public Card[] playerDeck; //the Card is the scriptable object, here I made it an array because a list was giving problems
public int deckIndex;
public PlayerData (Deck deck)
{
playerDeck = new Card[deck.GetDeckCards().Count];
for (int i = 0; i < deck.GetDeckCards().Count; i++)
{
playerDeck _= deck.GetDeckCards()*;*_
}
deckIndex = deck.GetCurrentDeckIndex();
}
}
Thank you for taking a look.