U
12 / 22 · 8 min
FAQ

Prefabs: the file, the copies, and the edits in between

the short answer

What is a prefab in Unity?

A prefab is a GameObject saved as a file in the Project panel; the copies in your scenes all follow it, so editing the file changes every one of them. Edit a copy instead and the change is an override on that copy alone — shown in bold, with Apply All and Revert All in the Overrides dropdown.

A prefab is a GameObject saved as a file. The copies you drop into scenes are not copies of each other — they all follow that one file, so changing it changes all of them at once. That is the whole idea, and everything confusing about prefabs comes from one question: am I editing the file, or one copy of it?

Making one

  1. 1

    Build the object in the scene until it is what you want: mesh, material, collider, scripts, children, all of it.

  2. 2

    Drag it from the Hierarchy into a folder in the Project panel. Its name in the Hierarchy turns blue: that copy is now an instance of a file on disk.

  3. 3

    Drag the file back into the scene as many times as you like. Twenty enemies, one file.

Editing the file, not one copy

Double-click the prefab in the Project panel and Unity opens Prefab Mode: the object alone, on an empty stage, with nothing else in the way. Whatever you change there is the file, so every instance in every scene follows immediately.

  • Change an instance in the scene instead, and you get an override: that one property, on that one copy, now ignores the file. The Inspector shows it in bold with a blue line in the margin.

  • The Overrides dropdown at the top of the Inspector lists every one of them, and gives you the two buttons that matter: Apply All, which writes them back into the file, and Revert All, which throws them away.

  • Overrides are not a mistake. A patrol point, a speed, a colour that differs per copy is exactly what they are for — but a copy with thirty of them is a prefab in name only.

Variants, for the enemy that is almost the same

A Prefab Variant is a prefab based on another one: it keeps everything from its parent and changes a few things on top. Make the base Enemy, then a Fast Enemy variant that only changes speed and colour — fix a bug in the base and both are fixed.

  • Right-click the prefab in the Project panel → Create▸Prefab Variant, then open the variant and change what makes it different.

  • A prefab can also sit inside another prefab — a wheel inside a car — and each keeps its own link. Unity calls that a nested prefab, and it behaves exactly like the outer one.

Spawning one from code

Spawner.cs — the field holds the file, never the copy in the scene
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public GameObject enemyPrefab;   // drag the asset from the Project panel

    void Start()
    {
        Instantiate(enemyPrefab, transform.position, Quaternion.identity);
    }
}

Drag the file from the Project panel into that slot — not the copy standing in the Hierarchy. Point it at a scene object and everything works until that object is deleted or disabled, which is the bug behind half the broken spawners ever written. Instantiate has a guide of its own.

Two states worth recognising

  • A red object named Missing Prefab means the file it followed was deleted or moved outside Unity. The object in the scene is an empty shell; put the file back, or delete the shell.

  • Prefab > Unpack breaks the link on purpose: you keep the objects, and they stop following the file. Useful once, for a piece of scenery you will never reuse; regrettable everywhere else.

the lesson that builds this

Block prefabs

This post is a standalone recipe. In the course, the same thing is built as part of the one project that runs through all five tabs — Basics, lesson 07.