I have tried adding a save system:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class MainInventory : MonoBehaviour
{
public WorldScript world;
[System.Serializable]
public class MainInventoryStorage
{
public List<InventorySlotData> inventorySlotsData = new List<InventorySlotData>();
}
public MainInventoryStorage storage;
public List<InventorySlot> inventorySlots = new List<InventorySlot>();
public Item testItem;
public void Start ()
{
updateInventoryUI();
addTestItem();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
saveInventory();
} else if (Input.GetKeyDown(KeyCode.L))
{
loadInventory();
}
}
public void updateInventoryUI ()
{
for (int i = 0; i < inventorySlots.Count; i++)
{
inventorySlots[i].updateSlot(world.cloneItem(storage.inventorySlotsData[i].storedItem));
}
}
public void addTestItem()
{
testItem = world.cloneItem(world.gameItems[0]);
storage.inventorySlotsData[0].storedItem = testItem;
storage.inventorySlotsData[0].storedItem.itemName = "Test";
updateInventoryUI();
}
public void saveInventory ()
{
string testSave = JsonUtility.ToJson(storage);
PlayerPrefs.SetString("storage", testSave);
}
public void loadInventory ()
{
storage = JsonUtility.FromJson<MainInventoryStorage>(PlayerPrefs.GetString("storage"));
updateInventoryUI();
}
}
but when I hit L it gives error:
Argument Exception: The object you want to instantiate is null
Here are the rest of the scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class WorldScript : MonoBehaviour
{
public List<Item> gameItems = new List<Item>();
public Item cloneItem (Item toClone)
{
Item clone = Instantiate<Item>(toClone);
return clone;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
[CreateAssetMenu(fileName = "Item", menuName = "ScriptableObjects/Item", order = 1)]
public class Item : ScriptableObject
{
public string itemName, itemDescription, itemID;
public bool stackable = false;
public int stackSize, maxStackSize;
}
can someone please tell me what I am doing wrong?