Hi there
I’m looking for a way to save a player’s current inventory between scenes. Using a tutorial, I created a basci inventory system that is used to log which items a player picks up, display an Icon of that item and keep track of the quantity of said item (for example, a red berry bush may offer a yield of 3 or 5 berries).
This all works well, apart from when I go to the next scene and the inventory dissapears.
Currently, the only data that transfers is the current quantities of each of the inventory items (which I save using playerprefs). But could someone offer me guidance on what may be the best way to preserve the inventory data and repopulate the ‘backpack’ with item icons and values?
I’ve traied various methods (arrays and playerprefs), but I keep getting myself into knots and would really appreciate if anyone has any thoughts?
Thanks in advance (and apologies if my coding is inefficient or sloppy - still learning here so be gentle),
Miss McGeek
Item data script
public class Item : MonoBehaviour {
public int ID;
public int quantity;
public string type;
public string description;
public Sprite icon;
public bool pickedUp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Slot data script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Slot : MonoBehaviour {
public GameObject item;
public int ID;
public string type;
public string description;
public Text text;
public bool empty;
public Sprite icon;
private LevelManager theLevelManager;
// Use this for initialization
void Start()
{
theLevelManager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update () {
}
public void UpdateSlot()
{
this.GetComponent<Image>().sprite = icon;
this.GetComponentInChildren<Text>().text = "" + theLevelManager.currentCount;
}
}
Inventory management script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public bool inventoryEnabled;
public GameObject inventory;
private int allSlots;
private int enabledSlots;
private GameObject[] slot;
public GameObject slotHolder;
public GameObject itemPickedUp;
private PlayerController thePlayer;
private LevelManager theLevelManager;
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
theLevelManager = FindObjectOfType<LevelManager>();
allSlots = 16;
slot = new GameObject[allSlots];
for (int i = 0; i < allSlots; i++)
{
slot[i] = slotHolder.transform.GetChild(i).gameObject;
if (slot[i].GetComponent<Slot>().item == null)
{
slot[i].GetComponent<Slot>().empty = true;
}
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.B))
{
inventoryEnabled = !inventoryEnabled;
}
if (inventoryEnabled == true)
{
inventory.SetActive(true);
Time.timeScale = 0;
// thePlayer.canMove = false;
}
else
{
inventory.SetActive(false);
Time.timeScale = 1f;
// thePlayer.canMove = true;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Item")
{
itemPickedUp = other.gameObject;
Item item = itemPickedUp.GetComponent<Item>();
theLevelManager.IncreaseItemCount(itemPickedUp.GetComponent<Item>().ID, itemPickedUp.GetComponent<Item>().quantity);
AddItem(itemPickedUp, item.ID, item.type, item.description, item.icon, theLevelManager.currentCount);
}
}
void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon, int currentCount)
{
for (int i = 0; i < allSlots; i++)
{
if ((slot[i].GetComponent<Slot>().empty) || (itemPickedUp.GetComponent<Item>().ID == slot[i].GetComponent<Slot>().ID))
{
itemObject.GetComponent<Item>().pickedUp = true;
slot[i].GetComponent<Slot>().item = itemObject;
slot[i].GetComponent<Slot>().icon = itemIcon;
slot[i].GetComponent<Slot>().type = itemType;
slot[i].GetComponent<Slot>().ID = itemID;
slot[i].GetComponent<Slot>().description = itemDescription;
itemObject.transform.parent = slot[i].transform;
itemObject.SetActive(false);
slot[i].GetComponent<Slot>().UpdateSlot();
slot[i].GetComponent<Slot>().empty = false;
return;
}
}
}
}
Level manager script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour
{
public PlayerController thePlayer;
public int currentCount;
public int redBerryCount;
public int blueberryCount;
public int cottonCount;
// Use this for initialization
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
if (PlayerPrefs.HasKey("RedBerryCount"))
{
redBerryCount = PlayerPrefs.GetInt("RedBerryCount");
}
if (PlayerPrefs.HasKey("BlueberryCount"))
{
blueberryCount = PlayerPrefs.GetInt("BlueberryCount");
}
if (PlayerPrefs.HasKey("CottonCount"))
{
cottonCount = PlayerPrefs.GetInt("CottonCount");
}
}
void Update()
{
}
public void IncreaseItemCount(int ID, int quantity)
{
if (ID == 1)
{
redBerryCount = redBerryCount + quantity;
currentCount = redBerryCount;
PlayerPrefs.SetInt("RedBerryCount", redBerryCount);
}
if (ID == 2)
{
blueberryCount = blueberryCount + quantity;
currentCount = blueberryCount;
PlayerPrefs.SetInt("BlueberryCount", blueberryCount);
}
if (ID == 3)
{
cottonCount = cottonCount + quantity;
currentCount = cottonCount;
PlayerPrefs.SetInt("CottonCount", cottonCount);
}
}
}