Help with mobile inventory/equip system

I’ve watched countless tutorials on creating an inventory and equipment system but they are all for fps style games! I’m really struggling on implementing the style of system I need. I have a very basic base to work on, but not sure on what to do next.

I have an:

Item Class
ItemDatabase Class
Inventory Class
Equipment Slot
Equipped Class

Firstly, my game has a loadout scene and this is where you can equip characters and gear before attempting to clear a level. This is why most of the tutorials I’ve watched are of limited use to me.

The first 3 classes listed above are working. I can add an item to my inventory using its ID. The ItemDatabase and Inventory are both Lists of Item.

The EquipmentSlot just assigns the slot a type (weapon, armor etc) and an ID. The Equipped Class doesn’t contain much at the this point as I get lost round about here. I would like it so when a slot is clicked the inventory will appear with all the items that can be equipped to that slot. Then when the item is clicked it puts itself into the slot and gets greyed out, or something similar, in the inventory. My idea was also to have the equipped items added to a list so that I can use this list to instantiate everything when a playable scene is loaded. I’m not a complete newbie with lists but couldn’t figure out how to add and remove items to a list from another list (or would I just do a new Add () altogether even though the inventory already contains the item I need?)

Can somebody please give me some basic code to give me a nudge in the right direction on this? Been banging my head against a brick wall on this inventory system for a week now.

Still looking for possible help with this although I feel like I have made a lot of progress.

I now have 3 lists:

ItemDatabase
Inventory
EquippedItems

These lists all sit inside their own classes and are all of type . Using a GetItemByID () and AddToList () I am able to populate my inventory, and then in a similar fashion I can populate my EquippedItems.

I have a UI set up for my inventory which simply instantiates a slot and an image when an Item is added (currently initiated in start but I will add a shop that will do this). So for my inventory part I think I am pretty much there.

The next step is to get the items in my EquippedItems list to appear in the correct slot when they are equipped, using an equip button, and removing them using an unequip button. I have 8 equipment slots whose types are identified using an enum (Head, Chest, Hands, Legs, Feet, Weapon1, Weapon2, Weapon3). My issues:

  • How can I pass the itemID to the equip/unequip buttons (these are instantiated as part of the slot prefab in the inventory).

  • How can I ensure an item will only go into the correct slot? This would involve comparing 2 enums (itemType and slotType) and I’m not sure on how to do this.

  • With my weapon slots I would obviously need to click the relevant equip button and then the slot that I would like to equip the weapon to. Again, how should I go about doing this?

I’m starting to feel like I’m in way over my head here. I feel like I’m confident enough with the C# but I don’t really know how to implement this system for a mobile device and to work with Unitys UI effectively. ANY input whatsoever would be very much appreciated! Many thanks.

BaseItem

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

[System.Serializable]
public class BaseItem
{
    public enum ItemType
    {
        Head,
        Chest,
        Hands,
        Legs,
        Feet,
        Weapon,
        Resource,
        Consumable
    }

    public int itemID;
    public string itemName;
    public string itemDescription;
    public ItemType itemType;
    public Sprite itemIcon;
    public GameObject itemModel;

    public BaseItem (int id, string name, string description, ItemType type)
    {
        itemID = id;
        itemName = name;
        itemDescription = description;
        itemType = type;
        itemIcon = Resources.Load<Sprite> ("Items/Icons/" + itemName);
        itemModel = Resources.Load<GameObject> ("Items/Models/" + itemName);
    }

    public BaseItem ()
    {
    
    }
}

ItemDatabase

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

public class ItemDatabase : MonoBehaviour
{
    public List<BaseItem> itemDatabase = new List<BaseItem> ();

    private void Start ()
    {
        CreateDatabase ();
    }

    private void CreateDatabase ()
    {
        //Weapons
        itemDatabase.Add(new BaseItem(0, "12Gauge", "A basic shotgun", BaseItem.ItemType.Weapon));
        itemDatabase.Add(new BaseItem(1, "AK47", "A powerful rifle", BaseItem.ItemType.Weapon));
        itemDatabase.Add(new BaseItem(2, "MiniUzi", "A powerful rifle", BaseItem.ItemType.Weapon));
        itemDatabase.Add(new BaseItem(3, "RocketLauncher", "A powerful rifle", BaseItem.ItemType.Weapon));
        itemDatabase.Add(new BaseItem(4, "TrenchGun", "A powerful rifle", BaseItem.ItemType.Weapon));

        //Armor
        itemDatabase.Add(new BaseItem(1000, "LeatherArmor", "A lightweight body armor", BaseItem.ItemType.Chest));
        itemDatabase.Add(new BaseItem(1001, "IronHelmet", "The helmet of a true knight", BaseItem.ItemType.Head));

        //Consumables
        itemDatabase.Add(new BaseItem(2000, "MedKit", "Ooo look, health!", BaseItem.ItemType.Consumable));
        itemDatabase.Add(new BaseItem(2001, "Caffeine", "Use sparingly!", BaseItem.ItemType.Consumable));

        //Resources
        itemDatabase.Add(new BaseItem(3000, "Coal", "A jet black substance used for crafting", BaseItem.ItemType.Resource));
        itemDatabase.Add(new BaseItem(3001, "Diamond", "A beautiful gem used for crafting", BaseItem.ItemType.Resource));
    }

    public BaseItem GetItemByID (int id)
    {
        foreach (BaseItem item in itemDatabase)
        {
            if (item.itemID == id)
            {
                return item;
            }
        }

        return null;
    }
}

Inventory

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

public class Inventory : MonoBehaviour
{
    public GameObject inventoryPanel;
    public GameObject inventorySlotParent;
    public GameObject inventorySlot;
    public GameObject itemImage;

    public List<BaseItem> inventory = new List<BaseItem> ();

    private ItemDatabase itemDatabase;

    private void Start ()
    {
        itemDatabase = GameObject.FindGameObjectWithTag ("ItemDatabase").GetComponent<ItemDatabase> ();

        //Testing purposes only. Populates inventory with all items in itemdatabase
        for (int i = 0; i < itemDatabase.itemDatabase.Count; i++)
        {
            AddItemToInventoryList (itemDatabase.itemDatabase [i].itemID);
        }
    }

    public void AddItemToInventoryList (int id)
    {
        BaseItem itemToAdd = itemDatabase.GetItemByID (id);
        inventory.Add (new BaseItem (itemToAdd.itemID, itemToAdd.itemName, itemToAdd.itemDescription, itemToAdd.itemType));

        AddItemToSlot (itemToAdd.itemIcon);
    }

    private void AddItemToSlot (Sprite icon)
    {
        //Create an empty slot
        var slotInstance = Instantiate (inventorySlot);
        slotInstance.transform.SetParent (inventorySlotParent.transform);

        //Create an empty image in that slot
        var iconInstance = Instantiate (itemImage);
        iconInstance.transform.SetParent (slotInstance.transform);

        //Change image to correct item image
        itemImage.GetComponent<Image> ().sprite = icon;
    }

    //Used for adding items from the inventory into equipment list. Should be called when a suitable item is clicked from the inventory
    public BaseItem GetItemByID (int id)
    {
        foreach (BaseItem item in inventory)
        {
            if (item.itemID == id)
            {
                return item;
            }
        }

        return null;
    }
}

EquippedItems

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

public class EquippedItems : MonoBehaviour
{
    //List of equipped items
    public List<BaseItem> equippedItems = new List<BaseItem> ();

    private Inventory inventory;

    void Start ()
    {
        equippedInSlot = new GameObject[8];
        inventory = GameObject.FindGameObjectWithTag ("InventoryManager").GetComponent<Inventory> ();

        //Test Purposes, this function will be called by passing a parameter in using an equip button
        AddItemFromInventoryToEquipmentList (0);
        AddItemFromInventoryToEquipmentList (1);
        AddItemFromInventoryToEquipmentList (2);
        AddItemFromInventoryToEquipmentList (1000);
        AddItemFromInventoryToEquipmentList (1001);
    }

    public void AddItemFromInventoryToEquipmentList (int id)
    {
        BaseItem itemToAdd = inventory.GetItemByID (id);
        equippedItems.Add (new BaseItem (itemToAdd.itemID, itemToAdd.itemName, itemToAdd.itemDescription, itemToAdd.itemType));

    }
}

EquipmentSlot

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

public class EquipmentSlot : MonoBehaviour
{
    public enum SlotType
    {
        Head,
        Chest,
        Hands,
        Legs,
        Feet,
        Weapon
    }

    public SlotType slot;
    public GameObject itemImage;
}