I'm trying to save a list of scriptable objects that are items in a player's inventory. With ES3 this works fine if it's within in the same scene (I tested this within the update loop as you can see in the script). But as soon as I go to the next scene, the list is not null but empty. I made the player inventory a singleton to see if it had to do something with different instance ID's. I did some research and found that this could be an issue, but that didn't seem to help either, so I'll probably change it back.
This is my code:
Code: Select all
using UnityEngine;
using System.Collections.Generic;
namespace Munnica.THL.Inventories
{
public class PlayerInventory : MonoBehaviour
{
public static PlayerInventory Instance;
[SerializeField] private InventoryObject inventoryObject;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void LoadInventory()
{
if (ES3.KeyExists("Inventory"))
{
inventoryObject.container = (List<InventorySlot>)ES3.Load("Inventory");
print("Loaded Player Inventory");
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.PageDown)) { SaveInventory(); }
else if (Input.GetKeyDown(KeyCode.PageUp)) { LoadInventory(); }
else if (Input.GetKeyDown(KeyCode.Delete)) { inventoryObject.container.Clear(); }
}
public void AddItem(InventoryItem item, int quantity)
{
for (int i = 0; i < inventoryObject.container.Count; i++)
{
if (inventoryObject.container[i].item == item)
{
inventoryObject.container[i].AddAmount(quantity);
break;
}
}
inventoryObject.container.Add(new InventorySlot(item, quantity));
}
public InventoryObject GetInventory()
{
return inventoryObject;
}
public void SaveInventory()
{
ES3.Save("Inventory", inventoryObject.container);
print("Player Inventory saved!");
}
}
}