How to make interactable Inventory UI

New to Unity and followed a tutorial on how to setup a inventory UI. I created a Canvas with UI elements and a bunch of Slots. I can add items to the inventory I get from the ground, but have no idea how to drop, use, or rearrange the items I pickup. I have a script for Inventory_UI, Slot_UI, and inventory. I am trying to say on Left mouse click use the item and on right click drop the item. I just don’t know how to determine which slot I clicked on in the inventory to return the item im clicking.

I am not sure which script my code should go into or the best way to go about this. I am trying to use the new Input system and am unsure if this is the correct way to go about this. Any guidance would be greatly appreciated.

Slot_UI code I am trying UseItem to determine which item i clicked on.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.InputSystem;
public class Slot_UI : MonoBehaviour
{
    private PlayerActionControls playerActionControls;
    public Image itemIcon;
    public PlayerController player;
    public TextMeshProUGUI quantityText;
    private Slot_UI slot;
private void Awake(){
        playerActionControls = new PlayerActionControls();
    }
    private void OnEnable() {
        playerActionControls.Enable();
    }
    private void OnDisable() {
        playerActionControls.Disable();
    }
    void Start(){
        playerActionControls.UI.Click.performed += cxt => UseItem(cxt);
    }
    private void UseItem(InputAction.CallbackContext cxt){ 
            print(slot.itemIcon);
            Debug.Log("use item");
      
    }
    public void SetItem(Inventory.Slot slot){
        if(slot != null){
            itemIcon.sprite = slot.icon;
            itemIcon.color = new Color(1, 1, 1, 1);
            quantityText.text = slot.count.ToString();
        }
    }
    public void SetEmpty(){
        itemIcon.sprite = null;
        itemIcon.color= new Color(1, 1, 1, 0);
        quantityText.text = "";
    }
}

Inventory_ui script

using System.Collections;
[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Inventory_UI : MonoBehaviour
{
    public GameObject inventoryPanel;
    public Inventory inv;
    public PlayerController player;
    private PlayerActionControls playerActionControls;
    public List<Slot_UI> slots = new List<Slot_UI>();

  
    void Update()
    {
       
        if(Input.GetKeyDown(KeyCode.Tab)){
            ToggleInventory();
        }
    }
    public void ToggleInventory(){
        if(!inventoryPanel.activeSelf){
            inventoryPanel.SetActive(true);
            Setup();
        }
        else{
            inventoryPanel.SetActive(false);
        }
    }
    void Setup(){
        if(slots.Count == player.inventory.slots.Count){
           
            for(int i = 0; i < slots.Count; i++){
                if(player.inventory.slots[i].type != CollectableType.NONE){
                    slots[i].SetItem(player.inventory.slots[i]);
                }
                else{
                    slots[i].SetEmpty();
                }
            }
        }
    }
}

Inventory.cs Script

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[System.Serializable]
public class Inventory
{
    private PlayerActionControls playerActionControls;
    private Slot_UI slot;
    private void Awake(){
        playerActionControls = new PlayerActionControls();
    }
    private void OnEnable() {
        playerActionControls.Enable();
    }
    private void OnDisable() {
        playerActionControls.Disable();
    }
   
  [System.Serializable]
  public class Slot{
      public CollectableType type;
      public int count;
      public int maxAllowed;
      public Sprite icon;

      public Slot(){
          type = CollectableType.NONE;
          count = 0;
          maxAllowed = 99;
      }
    public bool CanAddItem(){
        if(count < maxAllowed){
            return true;
        }
            return false;
    }
    public void AddItem(Collectable item){
        this.type = item.type;
        this.icon = item.icon;
        count++;
    }
  }
  public List<Slot> slots = new List<Slot>();

  public Inventory(int numSlots){
      for(int i = 0; i < numSlots; i++){
          Slot slot = new Slot();
          slots.Add(slot);
      }
  }

  public void Add(Collectable item){
      Debug.Log("ADD ITEM");
      foreach(Slot slot in slots){
          if(slot.type == item.type && slot.CanAddItem()){
            Debug.Log("Adding "+item.type);
            slot.AddItem(item);
            return;
          }
      }
      foreach(Slot slot in slots){
          if(slot.type == CollectableType.NONE){
            slot.AddItem(item);
            return;
          }
      }
  }
}

You should set this aside and go do a tutorial that supports those things, learn how they work, then come back and implement it in your current inventory.

At the end of the day you basically are processing user intents: pick square up, drop square here, etc.

Also, just so you know, these things (character customization, inventories, shop systems) are fairly tricky hairy beasts, definitely deep in advanced coding territory. They contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Or… do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all… evolve it as you go! :slight_smile:

Breaking down a large problem such as inventory:

https://discussions.unity.com/t/826141/4

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - Star Manta on the Unity3D forums

I had tried to follow a different tutorial on that behavior but it is widly different and I am trying to use the new input system so was unsure. I learn best by fire so ill probably beat my head against the wall until it finally works. Thanks for the valuable input though its alot to think about.