Topic 3 of 15 9 min beginner

Prefabs & Variants

Reusable objects that stay in sync

A prefab is a GameObject saved as an asset. Place a hundred copies in your scenes, change the prefab once, and all hundred update. Without prefabs you would be editing every enemy in every scene by hand.

Asset vs instance

Keeping these two straight explains most prefab confusion:

ThingLives inNote
Prefab assetThe Project windowThe master copy, saved on disk
Prefab instanceA sceneA link back to the asset
OverrideOne instanceA local change that survives asset edits
VariantThe Project windowA prefab that inherits from another prefab
Blue icon in the Hierarchy means the object is a prefab instance. Plain grey means it is a loose object that exists only in that scene — and will not update when you change anything.

Making one

  1. 1
    Build the object in a scene

    Add every component it needs and set the values you want as defaults.

  2. 2
    Drag it into the Project window

    Unity saves it as a .prefab asset. The object in the scene becomes an instance of it, and its name turns blue.

  3. 3
    Reuse it

    Drag the asset into any scene, or spawn it from code with Instantiate.

Overrides

Change a value on one instance and that property becomes an override: it is shown in bold in the Inspector and will no longer follow the prefab asset.

This is useful and dangerous in equal measure. Useful, because one guard can patrol a different route. Dangerous, because an accidental override silently opts that instance out of future fixes.

  • Apply pushes the instance’s change up into the asset, so every instance gets it.
  • Revert throws the local change away and re-syncs with the asset.
  • The Overrides dropdown on the instance lists everything that has drifted.
If a prefab edit "does not do anything" on one particular object in the scene, check its Overrides dropdown first. An override on that property is almost always the reason.

Variants

A variant is a prefab based on another prefab. It inherits everything, then changes specific parts. Edit the base and every variant picks up the change — except where the variant deliberately differs.

This is the prefab answer to the composition question from the first topic. One Enemy base, then FastEnemy, ArmouredEnemy, and BossEnemy as variants that tweak stats and visuals. Fix a bug in the base and every enemy in the game is fixed.

Create one by right-clicking a prefab in the Project window and choosing Create → Prefab Variant.

Spawning from code

Instantiate creates a copy of a prefab at runtime. Expose the prefab as a serialised field and assign it in the Inspector.

Spawner.cs
C#
using UnityEngine;

public class Spawner : MonoBehaviour
{
    [SerializeField] private GameObject enemyPrefab;
    [SerializeField] private Transform spawnPoint;

    public void SpawnOne()
    {
        GameObject enemy = Instantiate(
            enemyPrefab,
            spawnPoint.position,
            spawnPoint.rotation
        );

        // The copy is a normal GameObject - configure it as usual
        enemy.GetComponent<Health>().SetMax(50);
    }
}
Do not use GameObject.Find or drag a scene object into the prefab field. A prefab reference must point at the asset in the Project window, otherwise it breaks the moment that scene unloads.

Instantiate is expensive

Creating and destroying objects allocates memory and eventually triggers garbage collection, which shows up as a stutter. For anything you spawn constantly — bullets, particles, damage numbers, enemies in a wave — reuse objects instead of recreating them.

That pattern is called object pooling: create a batch up front, deactivate instead of destroying, and hand them back out on demand.

C#
// Reuses an existing object - no allocation, no GC spike
GameObject bullet = pool.Get(muzzle.position, muzzle.rotation);

// ...later, when it hits something
pool.Release(bullet);
The Object Pool script in the library is a drop-in implementation of exactly this. Pair it with Projectile System for bullets.

Nested prefabs

Prefabs can contain other prefabs, and the inner ones keep their own link. A Turret prefab can contain a Barrel prefab; editing Barrel updates every turret.

How to structure a character prefab

A workable layout is a root object holding the gameplay components (health, movement, input), with the visual model as a child, and any effects or attachment points as further children.

Keeping the model as a separate child matters: when you re-export the mesh from Blender, you replace one child instead of rebuilding the prefab. It also keeps the root’s scale at (1, 1, 1), which the Transform topic explains is important for physics.

What to take away

  • A prefab asset is the master copy; scene objects are instances linked to it.
  • Overrides let one instance differ — and silently opt out of future edits.
  • Variants inherit from a base prefab, so shared fixes propagate.
  • Assign prefab references in the Inspector, never by finding scene objects.
  • Pool anything you spawn frequently rather than calling Instantiate in a loop.