TExcelTAD
Home › Unity with C# › Prefabs & Variants

Prefabs & Variants

A prefab is a GameObject saved as an asset (.prefab), with all its components, children and values. You place instances of it in scenes or spawn them from code. Edit the prefab once and every instance updates. Enemies, bullets, pickups, doors and UI widgets are all prefabs in a typical project.

Creating and editing

  1. Build the object in the scene (for example a Coin with a mesh, a collider and a Coin script).
  2. Drag it from the Hierarchy into a folder in the Project window. It turns blue in the Hierarchy: it is now a prefab instance.
  3. Double-click the prefab asset to open Prefab Mode, an isolated editing stage. Changes there apply to every instance.
  4. Changing a value on one instance in the scene creates an override, shown in bold with a blue bar in the Inspector. Use the Overrides drop-down at the top of the Inspector to Apply it to the prefab or Revert it.

Variants and nesting

  • A prefab variant is a prefab based on another one: right-click the prefab and choose Create > Prefab Variant. It inherits everything and stores only its own overrides. Enemy → Enemy_Fast (more speed, red material) → Enemy_Fast_Boss (more health).
  • A nested prefab is a prefab instance inside another prefab: a Door prefab inside a Room prefab. Editing Door updates every room.

The value an instance ends up with is resolved from the most specific level that sets it:

csharp · try it yourself
using System;
using System.Collections.Generic;

var enemy = new Prefab("Enemy", null, new() { ["speed"] = "3", ["health"] = "50", ["color"] = "grey" });
var fast  = new Prefab("Enemy_Fast", enemy, new() { ["speed"] = "6", ["color"] = "red" });
var boss  = new Prefab("Enemy_Fast_Boss", fast, new() { ["health"] = "400" });

var inScene = new Instance(boss);
inScene.Overrides["color"] = "gold";          // a scene override on this one instance

Show("before", inScene);
enemy.Values["health"] = "60";                 // designer edits the base prefab
enemy.Values["armor"] = "2";                   // and adds a new field
fast.Values["speed"] = "7";
Show("after base edits", inScene);
Console.WriteLine(
quot;health comes from: {inScene.Source("health")}"); Console.WriteLine(
quot;armor comes from: {inScene.Source("armor")}"); static void Show(string label, Instance i) { Console.Write(
quot;{label,-17}"); foreach (var key in new[] { "speed", "health", "color", "armor" }) Console.Write(
quot; {key}={i.Get(key) ?? "-"}"); Console.WriteLine(); } class Prefab { public string Name; public Prefab? Base; public Dictionary<string, string> Values; public Prefab(string name, Prefab? b, Dictionary<string, string> values) { Name = name; Base = b; Values = values; } public (string?, string) Lookup(string key) => Values.TryGetValue(key, out var v) ? (v, Name) : Base?.Lookup(key) ?? (null, "nowhere"); } class Instance { public Prefab Prefab; public Dictionary<string, string> Overrides = new(); public Instance(Prefab p) => Prefab = p; public string? Get(string key) => Overrides.TryGetValue(key, out var v) ? v : Prefab.Lookup(key).Item1; public string Source(string key) => Overrides.ContainsKey(key) ? "scene override" : Prefab.Lookup(key).Item2; }
before            speed=6 health=400 color=gold armor=-
after base edits  speed=7 health=400 color=gold armor=2
health comes from: Enemy_Fast_Boss
armor comes from:  Enemy

The boss kept its own health (400) when the base changed to 60, picked up the new armor field, and took the variant's new speed. This is exactly how Unity resolves prefab values, and why stray overrides are a common bug: an instance that "doesn't update" usually has an override you forgot about.

Spawning prefabs from code

csharp
using UnityEngine;

public class CoinSpawner : MonoBehaviour
{
    [SerializeField] Coin coinPrefab;              // drag the prefab asset here; typed as the component you need
    [SerializeField] Transform[] spawnPoints;

    void Start()
    {
        foreach (Transform point in spawnPoints)
        {
            Coin coin = Instantiate(coinPrefab, point.position, Quaternion.identity, transform);
            coin.Value = 10;                       // configure the new instance
        }
    }
}

Typing the field as the component (Coin) instead of GameObject means Instantiate returns that component directly, and the Inspector only accepts prefabs that have it. Unity 6 also has InstantiateAsync, which spreads the cost of instantiating many or large objects across frames.

Tip: Never edit or destroy the prefab asset at runtime (the field coinPrefab itself). It changes the asset in the editor and the change survives leaving Play mode. Always work on the instance that Instantiate returned.

For objects spawned and destroyed constantly (bullets, hit effects), reuse them with a pool instead: see Object Pooling.

Test yourself

A prefab variant Orc_Archer overrides range. The base Orc prefab gets a new armor field. What does Orc_Archer show?

  1. Only range; new base fields are ignored
  2. Its own range and the inherited new armor
  3. An error until you re-create the variant
  4. The base range value

An instance in a scene ignores a change you made in Prefab Mode. What is the most likely cause?

  1. Prefabs only update after restarting Unity
  2. That instance has an override for that property
  3. The scene must be rebuilt
  4. Prefab Mode edits only apply to new instances

Why type a prefab field as [SerializeField] Coin coinPrefab rather than GameObject?

  1. It makes the prefab load faster
  2. Instantiate returns the Coin component directly and the Inspector only accepts prefabs that have one
  3. GameObject fields can't hold prefabs
  4. It pools instances automatically