Hi! I’m trying save scriptable object inventory with JSON utility and sometimes give me error at the start.
I think the problem is in the “load” function but im not sure…
For the moment it is working with Iformater, but i prefer use JSON.
What am i doing wrong? A part of the error, the component is completely reset after start. For example, there is another scriptable object related in the editor (my item database), and this is removed after error.
I let save and load functions with IFormater and JSON utility. (JSON it is comment because does not work)
[ContextMenu("Save")]
public void Save()
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, Container);
stream.Close();
//string saveData = JsonUtility.ToJson(this, true);
//BinaryFormatter bf = new BinaryFormatter();
//FileStream file = File.Create(string.Concat(Application.persistentDataPath, savePath));
//bf.Serialize(file, saveData);
//file.Close();
}
[ContextMenu("Load")]
public void Load()
{
if (File.Exists(string.Concat(Application.persistentDataPath, savePath)))
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Open, FileAccess.Read);
Inventory newContainer = (Inventory)formatter.Deserialize(stream);
for (int i = 0; i < Container.Items.Length; i++)
{
Container.Items[i].UpdateSlot(newContainer.Items[i].item, newContainer.Items[i].amount);
}
stream.Close();
//BinaryFormatter bf = new BinaryFormatter();
//FileStream file = File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
//string data = bf.Deserialize(file).ToString();
//JsonUtility.FromJsonOverwrite(data, this);
//file.Close();
}
else
{
//Clear();
Save();
}
}
Thank you in advance!