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:
| Thing | Lives in | Note |
|---|---|---|
Prefab asset | The Project window | The master copy, saved on disk |
Prefab instance | A scene | A link back to the asset |
Override | One instance | A local change that survives asset edits |
Variant | The Project window | A prefab that inherits from another prefab |
Making one
- 1 Build the object in a scene
Add every component it needs and set the values you want as defaults.
- 2 Drag it into the Project window
Unity saves it as a
.prefabasset. The object in the scene becomes an instance of it, and its name turns blue. - 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.
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.
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.
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);
}
}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.
// 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);// Allocates every shot, and garbage collects later
GameObject bullet = Instantiate(bulletPrefab, muzzle.position, muzzle.rotation);
// ...later
Destroy(bullet);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
Instantiatein a loop.