Simple Inventory

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

File
SimpleInventory.cs
Unity
2022.3+
Level
beginner
Size
1.9 KB · 115 lines
Added
2026-03-11
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 8 serialized fields with their defaults.

Inspector
Script SimpleInventory
Item Id
Quantity 0
Settings
Max Slots 20
Max Stack Size 99
Events
On Inventory Changed ()
List is Empty
On Item Added (string, int)
List is Empty
On Item Removed (string, int)
List is Empty
Items ItemEntry · Size 0

When to use it

Perfect for game jam prototypes, RPGs, survival games, and any project that needs basic item tracking without a full UI system. Ideal when you need quick add/remove/check operations. Pair with Pickup Collectible for world pickups and JSON Save Utility for persistence.

Setup

  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

Features

  • Add, remove, and check items with simple method calls
  • Configurable max slots and max stack size per item
  • UnityEvents for item added, removed, and inventory changed
  • Read-only Items list for safe UI binding
  • Automatic stacking of identical items up to max stack size
  • IsFull property and slot count tracking

Inspector fields

Every field Simple Inventory exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Item IditemIdstringempty
Quantityquantityint0
Max SlotsmaxSlotsint20Settings
Max Stack SizemaxStackSizeint99Settings
On Inventory ChangedOnInventoryChangedUnityEventNo listenersEvents
On Item AddedOnItemAddedUnityEvent<string, int>No listenersEvents
On Item RemovedOnItemRemovedUnityEvent<string, int>No listenersEvents
ItemsitemsList<ItemEntry>EmptyEvents

Public API

What other scripts can call on SimpleInventory.

MemberReturnsDescription
AddItem(string itemId, int quantity = 1)intAdd items to inventory. Returns the amount actually added.
RemoveItem(string itemId, int quantity = 1)intRemove items from inventory. Returns the amount actually removed.
HasItem(string itemId, int quantity = 1)boolCheck if inventory contains at least 'quantity' of an item.
GetItemCount(string itemId)intGet the quantity of an item in inventory.
Clear()voidClear all items from inventory.
Items propertyIReadOnlyList<ItemEntry>Read-only access to inventory items.
SlotCount propertyintNumber of unique item stacks.
IsFull propertyboolCheck if inventory is full (all slots used).

Common mistakes

Don't forget to set maxSlots and maxStackSize in the Inspector — the defaults (20 slots, 99 stack) may not fit your game. AddItem returns 0 if the inventory is full, so always check the return value. If OnInventoryChanged isn't firing, make sure you're calling AddItem/RemoveItem through the script rather than modifying the list directly.

Source code

SimpleInventory.cs, 115 lines, MIT licensed. Paste it into a new C# file of the same name.

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;
}