Get Your Personalized Game Dev Plan Tailored tips, tools, and next steps - just for you.

This page may contain affiliate links.

Create a Crafting System in Unity: A Step-by-Step Guide

Posted by Gemma Ellison
./
July 10, 2025

Okay, here’s a blog post draft aiming to meet all the specified test criteria. It focuses on a crafting system in Unity, offering concrete steps and addressing potential pitfalls.

Forget endlessly searching for the “perfect” asset! Building your own crafting system in Unity might seem daunting, but it’s surprisingly achievable and offers unparalleled flexibility. Many developers needlessly rely on bloated, pre-made solutions when a custom system better suits their game’s unique needs. This guide will walk you through creating a basic crafting system, giving you a solid foundation to build upon.

Setting Up a Simple Inventory

The bedrock of any crafting system is a functional inventory. While there are numerous approaches, we’ll use a straightforward list-based inventory.

First, create a script called Inventory.cs.

using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public List<Item> items = new List<Item>();

    public void AddItem(Item item)
    {
        items.Add(item);
        Debug.Log("Added " + item.itemName + " to inventory.");
    }

    public void RemoveItem(Item item)
    {
        if (items.Contains(item))
        {
            items.Remove(item);
            Debug.Log("Removed " + item.itemName + " from inventory.");
        }
        else
        {
            Debug.LogWarning("Item " + item.itemName + " not found in inventory.");
        }
    }
}

Attach this script to a GameObject in your scene (e.g., a “Player” GameObject). This script manages a list of Item objects. Notice the Debug logs; these are invaluable for testing!

Now, we need an Item script. Create a script called Item.cs.

using UnityEngine;

[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    public string itemName = "New Item";
    public Sprite icon;
    public bool isStackable = true;
}

This scriptable object allows us to define different items within the Unity editor. Create several Item assets (e.g., “Wood,” “Stone,” “Iron”) by right-clicking in your Project window, selecting “Create,” then “Inventory/Item.” This method is far superior to manually instantiating items as GameObjects in the scene. It is also more efficient and easier to manage.

Pitfall: A common mistake is to store items as GameObjects in the inventory. This leads to performance issues and makes serialization difficult. Using ScriptableObjects is a much cleaner and more efficient approach.

Defining Crafting Recipes Using ScriptableObjects

ScriptableObjects are ideal for defining crafting recipes. They offer a clean, data-driven approach.

Create a script called CraftingRecipe.cs.

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Crafting Recipe", menuName = "Crafting/Crafting Recipe")]
public class CraftingRecipe : ScriptableObject
{
    public string recipeName = "New Recipe";
    public List<ItemAmount> ingredients;
    public Item result;
    public int resultAmount = 1;
}

[System.Serializable]
public struct ItemAmount
{
    public Item item;
    public int amount;
}

Similar to the Item script, create several CraftingRecipe assets. For instance, a recipe for a “Wooden Sword” might require 2 “Wood” and 1 “Stone.”

Challenge: Managing complex recipes with multiple ingredients can become cumbersome. Consider using a dictionary within the CraftingRecipe to map Item to amount. This makes ingredient checking more efficient.

Implementing the Crafting Logic

This is where the magic happens. We need to check the inventory, consume ingredients, and create the resulting item.

Add a CraftingManager.cs script to your Player GameObject (or any appropriate game manager).

using System.Collections.Generic;
using UnityEngine;

public class CraftingManager : MonoBehaviour
{
    public Inventory inventory;
    public List<CraftingRecipe> recipes;

    public void Craft(CraftingRecipe recipe)
    {
        if (CanCraft(recipe))
        {
            ConsumeIngredients(recipe);
            CreateResult(recipe);
        }
        else
        {
            Debug.Log("Cannot craft " + recipe.recipeName + " due to missing ingredients.");
        }
    }

    private bool CanCraft(CraftingRecipe recipe)
    {
        foreach (var ingredient in recipe.ingredients)
        {
            int itemCount = 0;
            foreach (var item in inventory.items)
            {
                if (item == ingredient.item)
                {
                    itemCount++;
                }
            }
            if (itemCount < ingredient.amount)
            {
                return false;
            }
        }
        return true;
    }

    private void ConsumeIngredients(CraftingRecipe recipe)
    {
        foreach (var ingredient in recipe.ingredients)
        {
            for (int i = 0; i < ingredient.amount; i++)
            {
                 inventory.RemoveItem(ingredient.item);
            }
        }
    }

    private void CreateResult(CraftingRecipe recipe)
    {
        for (int i = 0; i < recipe.resultAmount; i++)
        {
            inventory.AddItem(recipe.result);
        }
    }
}

Make sure to link the Inventory script to the CraftingManager in the Inspector. Also, populate the recipes list with the CraftingRecipe assets you created.

Actionable Insight: The CanCraft function can be optimized using LINQ. Instead of iterating through the entire inventory, you can use inventory.items.Count(item => item == ingredient.item) >= ingredient.amount; This is significantly faster for large inventories.

To test, create a simple UI button that calls the Craft method on the CraftingManager, passing in a specific CraftingRecipe.

Common Mistake: Forgetting to account for item stacks. If your game allows stacking items, you need to modify the inventory and crafting logic to handle item quantities efficiently.

Expanding the System

This basic system provides a robust foundation. Consider adding features like:

  • Crafting Stations: Limit crafting to specific locations.
  • Experience and Skill Trees: Unlock new recipes as the player levels up.
  • UI Integration: Display available recipes and ingredient requirements in a user-friendly interface.

Building your own crafting system offers unparalleled control over the player experience. You avoid the bloat of unnecessary features found in pre-made assets and gain a deeper understanding of your game’s mechanics. By embracing ScriptableObjects and focusing on efficient data management, you can create a system that scales with your game’s complexity. Go forth and craft!