Make an object appear in an inventory

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.

3076423--231478--Inventory.jpg
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.

It appears that the inventory is coded so that, instead of adding a new object to the inventory, it increases the count of that item that are there. Do you have any way of showing the count of items? It’s possible that it’s working as advertised, you just don’t have any way of showing that there are 2 widgets instead of just 1.

Also: Don’t use GameObject.Find. This is a good place to use a singleton instead.

Thank you for your reply,

Indeed, the CheckifItemIsInInventory function increases a counter below the object if there are more than one.
So it’s impossible to do what I want to do because the code does not allow it?

3076550--231481--Preuve.jpg

What can I use other than: Do not use GameObject.Find?

Read the linked article - it outlines a number of options for different situations. This one probably calls for a singleton.

Check line 43 of your first script - it looks like it only stacks items like this if isStackable is true, which I assume would be an option you can disable in your item.

1 Like

Ok, I’ll try to change and see what happens without great hopes ^^.

Still thank you for your time

Hello !
Finally I managed to find my happiness by changing these few lines of the first code!

if (items[i].ID == -1)
                {

                   
                    items[i] = itemToAdd;
                    GameObject itemObj = Instantiate(inventoryItem);
                    itemObj.GetComponent<ItemData>().item = itemToAdd;
                    itemObj.GetComponent<ItemData>().amount = 1;
                    itemObj.GetComponent<ItemData>().slot = i;
                    itemObj.transform.SetParent(slots[i].transform);
                   // itemObj.transform.position = Vector2.zero;
                    itemObj.GetComponent<RectTransform>().offsetMin = new Vector2(0, 0);
                    itemObj.GetComponent<RectTransform>().offsetMax = new Vector2(40, 40);
                    itemObj.transform.position = slots[i].transform.position;
                    itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                    itemObj.name = itemToAdd.Title;
                    break;
                }

Have a good day