Part of these game systems:
beginner Inventory

Simple Inventory

Lightweight list-based inventory for prototypes. Add, remove, and check items without UI complexity.

Unity 2022.3+ · 1.9 KB · SimpleInventory.cs

How to Use

1

Attach SimpleInventory to your player

2

Add items: inventory.AddItem("sword", 1)

3

Remove items: inventory.RemoveItem("coin", 5)

4

Check items: inventory.HasItem("key")

5

Get count: inventory.GetItemCount("arrow")

6

Hook OnInventoryChanged to refresh your UI

7

For full drag-and-drop UI, use the Complete Inventory System instead

Source Code

SimpleInventory.cs
C#
using UnityEngine;
using UnityEngine.Events;
using System.Collections.Generic;

/// <summary>
/// Lightweight inventory system using a simple list.
/// Perfect for prototypes and jam games.
/// </summary>
public class SimpleInventory : MonoBehaviour
{
    [System.Serializable]
    public class ItemEntry
    {
        public string itemId;
        public int quantity;

        public ItemEntry(string id, int qty)
        {
            itemId = id;
            quantity = qty;
        }
    }

    [Header("Settings")]
    [SerializeField] private int maxSlots = 20;
    [SerializeField] private int maxStackSize = 99;

    [Header("Events")]
    public UnityEvent OnInventoryChanged;
    public UnityEvent<string, int> OnItemAdded;
    public UnityEvent<string, int> OnItemRemoved;

    [SerializeField] private List<ItemEntry> items = new List<ItemEntry>();

    /// <summary>Read-only access to inventory items.</summary>
    public IReadOnlyList<ItemEntry> Items => items;

    /// <summary>Number of unique item stacks.</summary>
    public int SlotCount => items.Count;

    /// <summary>
    /// Add items to inventory. Returns the amount actually added.
    /// </summary>
    public int AddItem(string itemId, int quantity = 1)
    {
        int added = 0;

        // Try to stack with existing
        ItemEntry existing = items.Find(i => i.itemId == itemId);
        if (existing != null)
        {
            int canAdd = Mathf.Min(quantity, maxStackSize - existing.quantity);
            existing.quantity += canAdd;
            added = canAdd;
        }
        else if (items.Count < maxSlots)
        {
            int toAdd = Mathf.Min(quantity, maxStackSize);
            items.Add(new ItemEntry(itemId, toAdd));
            added = toAdd;
        }

        if (added > 0)
        {
            OnItemAdded?.Invoke(itemId, added);
            OnInventoryChanged?.Invoke();
        }

        return added;
    }

    /// <summary>
    /// Remove items from inventory. Returns the amount actually removed.
    /// </summary>
    public int RemoveItem(string itemId, int quantity = 1)
    {
        ItemEntry existing = items.Find(i => i.itemId == itemId);
        if (existing == null) return 0;

        int removed = Mathf.Min(quantity, existing.quantity);
        existing.quantity -= removed;

        if (existing.quantity <= 0)
            items.Remove(existing);

        OnItemRemoved?.Invoke(itemId, removed);
        OnInventoryChanged?.Invoke();

        return removed;
    }

    /// <summary>Check if inventory contains at least 'quantity' of an item.</summary>
    public bool HasItem(string itemId, int quantity = 1)
    {
        ItemEntry entry = items.Find(i => i.itemId == itemId);
        return entry != null && entry.quantity >= quantity;
    }

    /// <summary>Get the quantity of an item in inventory.</summary>
    public int GetItemCount(string itemId)
    {
        ItemEntry entry = items.Find(i => i.itemId == itemId);
        return entry?.quantity ?? 0;
    }

    /// <summary>Clear all items from inventory.</summary>
    public void Clear()
    {
        items.Clear();
        OnInventoryChanged?.Invoke();
    }

    /// <summary>Check if inventory is full (all slots used).</summary>
    public bool IsFull => items.Count >= maxSlots;
}