Hello guys,
I wrote this code when creating an inventory system. I can add items to the inventory fine but when I click “I” to escape the inventory or to enter it, it wont open/ close. If you you are wandering how I can see if the items are being added I have done this by showing my canvas when it should be off to see if at least the item will be added. Any help is appreciated. Thanks!
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public GameObject inventory;
public GameObject slotHolder;
private bool inventoryEnabled;
private int slots;
private Transform[] slot;
private GameObject itemPickedUp;
private bool itemAdded;
public void Start()
{
// slots being detected
slots = slotHolder.transform.childCount;
slot = new Transform[slots];
DetectInventorySlots();
}
public void update()
{
if (Input.GetKeyDown(KeyCode.I))
{
inventoryEnabled = !inventoryEnabled;
}
if (inventoryEnabled)
inventory.SetActive(true);
else
inventory.SetActive(false);
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Item")
{
itemPickedUp = other.gameObject;
AddItem(itemPickedUp);
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Item")
{
itemAdded = false;
}
}
public void AddItem(GameObject item)
{
for (int i = 0; i < slots; i++)
{
if (slot[i].GetComponent<Slot>().empty && itemAdded == false)
{
slot[i].GetComponent<Slot>().item = itemPickedUp;
slot[i].GetComponent<Slot>().itemIcon = itemPickedUp.GetComponent<Item>().icon;
itemAdded = true;
}
}
}
public void DetectInventorySlots()
{
for (int i = 0; i < slots; i++)
{
slot[i] = slotHolder.transform.GetChild(i);
}
}
}