- Home /
Question by
zxubian · Oct 22, 2015 at 08:02 PM ·
arrayserializationscriptableobjectsavegame
Serializing scriptableobjects array for savegame
I'm having trouble serializing an array of scriptableobjects (each is a sprite and a string).
I'm very new to serialization and I started building a serializer class based on the one in the tutorial.
Running this code:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
public class SaveManager : MonoBehaviour {
public void Save()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
SaveData data = new SaveData();
data.inventory = new InventoryItem[InventoryManager.Instance.inventorySize];
for (int i = 0; i<InventoryManager.Instance.inventorySize; i++) {
if(InventoryManager.Instance.inventory[i]!=null)
data.inventory[i]=InventoryManager.Instance.inventory[i];
}
bf.Serialize (file, data);
file.Close();
}
}
[Serializable]
class SaveData{
public InventoryItem[] inventory;
//public Sprite[] inventory_sprites;
//public Quest[] quests;
public float[] playerPosition;
}
Gives the following error:
SerializationException: Type UnityEngine.ScriptableObject in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
The InventoryItem class is set as [System.Serializable].
Comment
Your answer
Follow this Question
Related Questions
Board game minimax AI implementation 0 Answers
What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 Answers
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers
Unity Not Loading 0 Answers
Why do I need to serialize a struct inside a ScriptableObject to save its data? 1 Answer