Hello,
I followed a tutorial in order to make an inventory https://www.youtube.com/playlist?list=PLivfKP2ufIK78r7nzfpIEH89Nlnb__RRG .
Everything is fine, I managed to create it, but now I want to make objects appear in this inventory.
The object does not appear in this inventory if and only if an object similar to it is already there.
If it is not there, the image of the object that should appear in an inventory box appears in the center of the collider to which the OnTriggerEnter … script is put.
I simply do not have to put 1 object of each in the inventory so that it can appear if one retrieves it in the game.
I hope to make myself understood and that you will be able to help me.
Have a good day
Gauthier
The first code used to display and use the inventory.

Image of inventory in game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
GameObject inventoryPanel;
GameObject slotPanel;
ItemDatabase database;
public GameObject inventorySlot;
public GameObject inventoryItem;
int slotAmount;
public List<Item> items = new List<Item>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<ItemDatabase>();
slotAmount = 16;
inventoryPanel = GameObject.Find("Inventory Panel");
slotPanel = inventoryPanel.transform.FindChild("Slot Panel").gameObject;
for (int i = 0; i < slotAmount; i++)
{
items.Add(new Item());
slots.Add(Instantiate(inventorySlot));
slots[i].GetComponent<Slot>().id = i;
slots[i].transform.SetParent(slotPanel.transform);
}
}
public void AddItem(int id)
{
Item itemToAdd = database.FetchItemByID(id);
if (itemToAdd.Stackable && CheckIfItemIsInInventory(itemToAdd))
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == id)
{
ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
data.amount++;
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
break;
}
}
}
else
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == -1)
{
items[i] = itemToAdd;
GameObject itemObj = Instantiate(inventoryItem);
itemObj.GetComponent<ItemData>().item = itemToAdd;
itemObj.GetComponent<ItemData>().amount = i;
itemObj.GetComponent<ItemData>().slot = i;
itemObj.transform.SetParent(slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
break;
}
}
for (int i = 0; i < slotAmount; i++)
{
items.Add(new Item());
slots.Add(Instantiate(inventorySlot));
slots[i].GetComponent<Slot>().id = i;
slots[i].transform.SetParent(slotPanel.transform);
}
}
}
bool CheckIfItemIsInInventory(Item item)
{
for (int i = 0; i < items.Count; i++)
if (items[i].ID == item.ID)
return true;
return false;
}
}
And the second code to make appear an object in the inventory that is on a collider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ObjInv : MonoBehaviour {
Inventory database;
void Start()
{
database = GameObject.Find("Inventory").GetComponent<Inventory>();
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
database.AddItem(1);
}
}
}
Thank you for taking the time to answer me.
