Block prefabs
Assets/
Scenes/
Arena.unity
Prefabs/
Materials/
you already havethis lesson adds
A prefab is a saved GameObject you can stamp out as many times as you like. Edit the original and every copy in every scene updates.
One prefab asset in the Project window, many instances in the scene.
Make the block prefabs
- 1
. Reset its Transform, set Position to 0, 0.5, 0.
- 2
Why 0.5? A default cube is 1 metre tall and its pivot is in the centre, so half of it must be above the floor.
- 3
Create a material M_Block, tint it, drag it onto the cube.
- 4
Drag the cube from the Hierarchy into the Assets/Prefabs folder you made in lesson 05. The name turns blue — it is now a prefab.
- 5
Repeat for Sphere, Capsule and Cylinder. Four block prefabs are plenty.
Editing a prefab
✓ Edit the prefab asset
✖ Edit one instance
In the Inspector, the Overrides dropdown shows what you changed. Apply pushes it back to the prefab; Revert throws it away.
Right-click a prefab → Create → Prefab Variant when you want a version that inherits but differs (a red cube from a grey cube).
Snap while you place
Hold Ctrl (Windows) / Cmd (Mac) while dragging to snap in fixed steps.
Set the step in — 1 metre is right for block building.
Hold V to grab a vertex and snap it exactly onto another object's vertex.
Referencing a prefab from code
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private GameObject blockPrefab; // drag P_Cube here
void Start()
{
// (prefab, position, rotation)
Instantiate(blockPrefab, new Vector3(2f, 0.5f, 3f), Quaternion.identity);
}
}Quaternion.identity means no rotation. It is Unity's way of writing 0, 0, 0 for angles.
Drag the prefab from Project (not from Hierarchy) into that Inspector slot.
Common mistakes
The name never turned blue: you duplicated the cube with Ctrl+D instead of dragging it into Assets/Prefabs. A duplicate is a second scene object, not an asset. Only the drag into the Project window creates the prefab.
You changed the red cube in the scene and the other ninety-nine stayed grey: that edit was an override on one instance. Open the Overrides dropdown and Apply All, or edit the asset in prefab mode instead.
Dragging the scene copy into the Inspector slot instead of the asset works until it does not: Instantiate clones that instance with its overrides, and the reference is null in any other scene. Always drag from the Project window.