Hi I need some help. Need Help WIth Inventory System and Using Items

Hi Im very much still a Newby at this but I made an Inventory System and i would like to use some of the items to give me health if i click on it in my inventory.

here is my Inventory, the Item and not sure if you need it but thebuttonItem i use for removing the items from inventory.

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;

public class gameManager : MonoBehaviour
{
    public static gameManager instance;
    public bool isPaused;
    public List<Item> items = new List<Item>();// WHAT KIND OF ITEMS
    public List<int> itemNumbers = new List<int>();//HOW MANY WE HAVE
    public GameObject[] slots;
    //public.Dictionary<Item, int> itemDict = new Dictionary<Item, int>();//OPTIONAL
    //public Item addItem_01;//TO DO REMOVE LATER
    //public Item removeItem_01;//TO DO REMOVE LATER
    public ItemButton thisButton;//KEEP TRACK OF WHICH ITEM WE ARE HOVERING OVER
    public ItemButton[] itemButtons;//ALL THE ITEM BUTTONS IN GAME[USED FOR RESET]
 

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            if (instance != this)
            {
                Destroy(gameObject);
            }
        }DontDestroyOnLoad(gameObject);
    }

    public void Start()
    {
        DisplayItems();
     
    }
    private void DisplayItems()
    {
        #region
        /*for (int i = 0; i < items.Count; i++)
        {
            //UPDATE SLOTS ITEM IMAGE
            slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
            slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;

            //UPDATE SLOTS COUNT TEXT
            slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
            slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();

            //UPDATE CLOSE/THROW BUTTON
            slots.transform.GetChild(2).gameObject.SetActive(true);
        }*/
        #endregion

        //WE IGNORE THE FACT
        for (int i = 0; i < slots.Length; i++)
        {
            if (i < items.Count)
            {
                //UPDATE SLOTS ITEM IMAGE
                slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
                slots.transform.GetChild(0).GetComponent<Image>().sprite = items.itemSprite;

                //UPDATE SLOTS COUNT TEXT
                slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 1f);
                slots.transform.GetChild(1).GetComponent<Text>().text = itemNumbers.ToString();

                //UPDATE CLOSE/THROW BUTTON
                slots.transform.GetChild(2).gameObject.SetActive(true);
            }
            else//SOME REMOVE ITEMS
            {
                //UPDATE SLOTS ITEM IMAGE
                slots.transform.GetChild(0).GetComponent<Image>().color = new Color(1f, 1f, 1f, 0f);
                slots.transform.GetChild(0).GetComponent<Image>().sprite = null;

                //UPDATE SLOTS COUNT TEXT
                slots.transform.GetChild(1).GetComponent<Text>().color = new Color(1f, 1f, 1f, 0f);
                slots.transform.GetChild(1).GetComponent<Text>().text = null;

                //UPDATE CLOSE/THROW BUTTON
                slots.transform.GetChild(2).gameObject.SetActive(false);
            }
        }
    }
    private void Update()
    {/*
        if (Input.GetKeyDown(KeyCode.M))
        {
            AddItem(addItem_01);
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            RemoveItem(removeItem_01);
        }*/
    }
     public void AddItem(Item _item)
     {
        //MARKER IF THERE IS ONE EXISTING ITEM IN OUR BAG/LIST
        if (!items.Contains(_item))
        {
            items.Add(_item);
            itemNumbers.Add(1);//ADD ONE
        }
        //IF THERE IS A NEW _ITEM IN OUR BAG
        else
        {
            UnityEngine.Debug.Log("You Already Have This One.");
            for (int i = 0; i < items.Count; i++)
            {
                if (_item == items)
                {
                    itemNumbers[i]++;
                }
            }
        }
        DisplayItems();
     }
     public void RemoveItem(Item _item)
    {
        //IF ONE IS EXISTING IN OUR INV
      
        if (items.Contains(_item))
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (_item == items[i])
                {
                  
                    itemNumbers[i]--;
                    if (itemNumbers[i] == 0)
                    {
                        //WE HAVE TO REMOVE THIS ITEM
                        items.Remove(_item);
                        itemNumbers.Remove(itemNumbers[i]);
                      
                    }
                }
              
            }
        }
        else
        {
            UnityEngine.Debug.Log("THERE IS NO" + _item + "IN YOUR INVENTORY");
        }
        //IF THERE IS NO OTHER ITEN IN INV
        ResetButtonItems();
        DisplayItems();
    }
    public void ResetButtonItems()
    {
        for (int i = 0; i < itemButtons.Length; i++)//FOR LOOP OF ALL BUTTONS TOTAL IS 14
        {
            if (i < items.Count)
            {
                itemButtons[i].thisItem = items[i];
            }
            else
            {
                itemButtons[i].thisItem = null;
            }
        }
    }
    public void Use()
    {
        //Item.Use();
    }


}



using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(menuName = "Item", fileName = "New Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public string itemDes;
  

    public Sprite itemSprite;
    public int itemPrice;




    public virtual void Use()
    {
       
    }

i have different item types if there was a way i could click on a button and it would determine what kind of item was in the slot and apply the effects to player or whatever… if that makes sense

@kdawgfsho04

Use code tags… :hushed: Using code tags properly

Also, isn’t this more like a generic scripting question?

But fix that code in your post so that it is possible to read it…

Anyway… I’m always personally interested in inventory related questions…

hey I’ve been working on a game for half a year, and have a decent inventory system that works well, however explaining it on the forms would be pretty slow (because its complicated). discord maybe? lubba64#5426. if not then I would recommend this YouTube tutorial: https://www.youtube.com/watch?v=2WnAOV7nHW0&ab_channel=CodeMonkey and one last thing, inventories are really hard to do well, so be careful and make sure you actually need one in your game before you attempt to make one.

the reason I’m not addressing your problem directly is because it looks like your code for the base inventory class could be improved significantly, because it looks like your slot, UI logic, and item distribution and storage logic are all in the same script (which can get messy and hard to maintain and update.)
if you want to make a health potion a potion system would be in order, which would also be hard to explain. I would recommend making a robust inventory first, then adding “potion” logic. I also have a potion system of my own but its unique to my project and system, so again if you wanna see my implementation contact me on discord Lubba64#5426

if you want to make potions make sure they can be “tagged” as a potion, give them some sort of identifier for what they should do (whether this be in a script or in your item storage solution) and make functions that you can assign to these identifiers so it can execute that function when the action for drinking a potion occurs.

sorry for the wall of text its a simple question which can easily have a complex answer

THanks for the reply… i messaged you on discord. :slight_smile:

:wink: no problem. for anyone else checking this thread here’s my inventory system explained in a YouTube video: