- Home /
Save and load an inventory
Hi everyone, I've already created the inventory and everything works fine. Now I should implement the saving and loading of the inventory (on file). However, I find myself stuck on how to proceed. Would you have any advice for me? Below is the inventory code.
public class Inventory2 : MonoBehaviour
{
public bool inventoryEnabled;
public GameObject inventory, slotHolder;
private Transform[] slot;
public int level;
void Start()
{
level = SceneManager.GetActiveScene().buildIndex;
GetAllSlots();
}
void Update()
{
if (Input.GetAxis("Inventory") != 0)
{
inventoryEnabled = !inventoryEnabled;
}
if(inventoryEnabled)
{
inventory.SetActive(true);
}
else
{
inventory.SetActive(false);
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Clues")
{
AddClue(other.GetComponent<Clue2>());
}
}
public void AddClue(Clue2 clue)
{
Text description = clue.GetComponent<Text>();
for (int i = 0; i < 2; i++)
{
if(slot[i].GetComponent<Slot2>().empty == true && clue.pickedUp == false)
{
slot[i].GetComponent<Slot2>().clue = clue;
slot[i].GetComponent<Slot2>().descriptionFinal = description;
slot[i].GetComponent<Slot2>().empty = false;
clue.GetComponent<Clue2>().pickedUp = true;
}
}
}
public void GetAllSlots()
{
slot = new Transform[Clue2.objects];
for(int i = 0; i < Clue2.objects; i++)
{
slot[i] = slotHolder.transform.GetChild(i);
slot[i].GetComponent<Slot2>().empty = true;
}
}
}
public class Slot2 : MonoBehaviour
{
public Clue2 clue;
public bool empty;
public Text descriptionFirst, descriptionFinal;
void Awake()
{
}
void Update()
{
if (clue)
{
this.GetComponentInChildren<Text>().text = descriptionFinal.text;
}
else
{
this.GetComponentInChildren<Text>().text = descriptionFirst.text;
empty = true;
}
}
}
public class Clue2 : MonoBehaviour
{
public static int objects = 0;
public static int objectsCollected = 0;
public Text description;
public bool pickedUp;
public GameObject cluePopUpPanel, canvasCluesPanel;
public Canvas canvasHUD;
public static bool activeClue = false;
void Awake()
{
objects++;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (canvasCluesPanel.gameObject.activeSelf == true && Input.GetAxis("PickUp") != 0)
{
activeClue = true;
cluePopUpPanel.gameObject.GetComponent<UnityEngine.UI.Text>().text = description.text;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (canvasCluesPanel.gameObject.activeSelf == false)
{
canvasCluesPanel.gameObject.SetActive(true);
}
}
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player" && activeClue == true)
{
cluePopUpPanel.gameObject.SetActive(true);
cluePopUpPanel.GetComponentInChildren<Text>().text = this.GetComponent<Text>().text;
canvasCluesPanel.gameObject.SetActive(false);
}
if (other.tag == "Player" && activeClue == false)
{
cluePopUpPanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(true);
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player" && activeClue == true)
{
cluePopUpPanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(false);
activeClue = false;
if(objectsCollected < objects)
{
objectsCollected++;
}
}
else
{
cluePopUpPanel.gameObject.SetActive(false);
canvasCluesPanel.gameObject.SetActive(false);
activeClue = false;
}
canvasHUD.gameObject.SetActive(true);
}
}
Comment