Menu
CoddyTech

Crafting Bench

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 49 of 50.

In a survival game, a crafting bench turns what you gather into things you can use. Two buttons gather wood and stone, and three recipes turn them into more: two wood make a plank, a plank and two stone make an axe, four planks make a raft. A recipe's button can be pressed only while the inventory holds everything the recipe needs, and crafting uses those things up. The recipes are data filled in on the bench in the Inspector, so a new recipe needs no new code. The inventory announces every change to whoever is listening, and it is saved as JSON each time it changes, so quitting the game loses nothing.

Write two scripts: Inventory, on the Backpack, and CraftingBench, on the Bench.

challenge icon

Challenge

Hard

Inventory:

  • A [System.Serializable] class ItemStack with public string item and public int count, and a [System.Serializable] class InventoryData with public List<ItemStack> stacks. The inventory keeps one InventoryData.
  • public event Action Changed, raised after every change.
  • public int Count(string item): how many of the item it holds, 0 when it has none.
  • public void Add(string item, int amount) adds to the item's stack, or adds a new stack at the end of the list. public void Remove(string item, int amount) takes from it, and a stack that reaches 0 leaves the list.
  • public string Describe() lists the stacks in order, as Wood 3, Plank 1, or returns empty.
  • Every change is saved at once to inventory.json in Application.persistentDataPath, as JsonUtility.ToJson of the InventoryData. Awake loads that file when it exists.

CraftingBench, which the Inspector gives inventory, recipes, recipeButtons (one per recipe, in the same order), woodButton, stoneButton and inventoryText:

  • A [System.Serializable] class Ingredient with public string item and public int amount, and a [System.Serializable] class Recipe with public string result and public List<Ingredient> needs. The bench's recipes is a List<Recipe>, filled in the scene.
  • The gather buttons add one Wood or one Stone. Each recipe button calls public void Craft(int index) with its recipe's index.
  • Craft does nothing when the inventory lacks anything the recipe needs. Otherwise it removes every ingredient, adds one of the result and logs it, as Crafted Axe.
  • At the start and whenever the inventory changes, each recipe button is clickable only while its recipe can be crafted, and inventoryText shows the description after the word Inventory, as Inventory: Wood 3, Plank 1 or Inventory: empty.

Press Play, gather and craft a raft, press Stop and Play again, then Check.

Try it yourself

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

public class CraftingBench : MonoBehaviour
{
    [SerializeField] private Inventory inventory;
    [SerializeField] private Button[] recipeButtons;
    [SerializeField] private Button woodButton;
    [SerializeField] private Button stoneButton;
    [SerializeField] private TMP_Text inventoryText;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Game Architecture in Unity