Hey guys, I’ve have totally no idea how to save a class with List of scriptableObjects
and here is the class that is being saved:
[System.Serializable]
public class Inventory
{
public float totalCash;
public float stoveUpgradeValue;
public float cashGenUpgradeValue;
public List<CounterData> counterDataList = new List<CounterData>();
public List<PurchasedItems> purchasedItemsList = new List<PurchasedItems>();
public List<PurchasedItems> ownedCountersList = new List<PurchasedItems>();
public List<PurchasedItems> ownedRecipeList = new List<PurchasedItems>();
}
and here is the PurchasedItems
class:
public string itemName;
public CounterSO counterSo;
public RecipeSO recipeSO;
public int amount;
and here is how i save
public void SaveToJson()
{
string inventoryData = JsonUtility.ToJson(inventory);
string filePath = Application.persistentDataPath + "/InventoryData.json";
inventory.totalCash += cashInPocket;
inventory.stoveUpgradeValue += upgradeManager.stoveCurrentLevel;
inventory.cashGenUpgradeValue += upgradeManager.cashGenCurrentLevel;
Debug.Log(filePath);
System.IO.File.WriteAllText(filePath, inventoryData);
}
public void LoadFromJson()
{
string filePath = Application.persistentDataPath + "/InventoryData.json";
string inventoryData = System.IO.File.ReadAllText(filePath);
inventory = JsonUtility.FromJson<Inventory>(inventoryData);
CashManager.Instance.currentCash = inventory.totalCash;
upgradeManager.stoveCurrentLevel = inventory.stoveUpgradeValue;
upgradeManager.cashGenCurrentLevel = inventory.cashGenUpgradeValue;
}
the target here is to save the Inventory class that has List
of PurchasedItems
class which contains scriptable objects but the problem every day i open unity the scriptable objects reference becomes type mismatch