Prefab
A Prefab is a reusable Unity template asset — design once, spawn many times. Change the Prefab and every instance that still inherits those properties updates.
Imagine...
A Prefab is like a bread mold. You design the mold (the Prefab asset) once with all the ingredients. Then you pour dough into it (Instantiate()) and get hundreds of identical loaves in seconds. Need a new flavor? Edit the mold — every future batch picks up the change.
The concept in detail
A Prefab is a .prefab file that stores an entire GameObject hierarchy —
Components, settings, and children. Drag it into a Scene and Unity creates a
Prefab Instance linked back to that asset.
Prefab Overrides: each Instance can override selected properties (color, HP…) while still inheriting everything else. When the Prefab asset updates, instances without an override pick up the new values automatically.
Nested Prefabs: a Prefab can contain other Prefabs. Handy for a Level Prefab that holds Room Prefabs, each Room holding Enemy Prefabs.
Calling Instantiate()/Destroy() in a tight loop causes lag
(Garbage Collection spikes). For high-frequency spawns (machine-gun bullets, VFX…) use
Object Pooling.
Diagram: Prefab → Instances
📦 Prefab Asset
Enemy_Goblin.prefab
⚙ Rigidbody · ⚙ Collider · ⚙ EnemyAI
HP: 100 · Speed: 3
Instance #1
pos: (2, 0, 5)
HP: 100 (from Prefab)
Instance #2 — Override
pos: (-3, 0, 7)
HP: 150 ← Override!
Instance #3
pos: (0, 0, 10)
HP: 100 (from Prefab)
Hands-on steps
Create a Prefab from the Scene
Finish the setup → drag from Hierarchy into an Assets folder in the Project window.
Edit in Prefab Mode
Double-click the .prefab file → isolated edit space. Changes propagate to all Instances.
Assign via [SerializeField]
In the Inspector, drag the Prefab asset from Project onto a field so your script can Instantiate it.
Apply Overrides to the Prefab asset
Select an Instance → Inspector → Overrides → Apply All.
Interactive simulator
Pick a Prefab type, press Instantiate to spawn a copy. Click an Instance to select it and change an Override. Press Destroy to remove it.
Press "Instantiate()" to spawn a GameObject from the Prefab
Inspector
Select an Instance
Code example
Basicusing UnityEngine;
public class PrefabSpawner : MonoBehaviour
{
[SerializeField] private GameObject enemyPrefab;
[SerializeField] private Transform spawnPoint;
void Start()
{
// Spawn a copy at the spawnPoint pose
GameObject clone = Instantiate(
enemyPrefab, spawnPoint.position, spawnPoint.rotation
);
// Configure the clone immediately
EnemyAI ai = clone.GetComponent<EnemyAI>();
if (ai != null)
{
ai.patrolSpeed = 3f;
ai.detectionRange = 8f;
}
clone.name = $"Enemy_{Random.Range(100, 999)}";
}
}Code example
Advancedusing System.Collections;
using UnityEngine;
public class WaveSpawner : MonoBehaviour
{
[SerializeField] private GameObject[] enemyPrefabs; // multiple enemy types
[SerializeField] private Transform[] spawnPoints; // multiple spawn points
[SerializeField] private int enemiesPerWave = 5;
[SerializeField] private float spawnInterval = 0.5f;
[SerializeField] private float waveCooldown = 5f;
private int _waveCount = 0;
void Start() => StartCoroutine(SpawnLoop());
private IEnumerator SpawnLoop()
{
while (true)
{
_waveCount++;
for (int i = 0; i < enemiesPerWave; i++)
{
// Pick a Prefab and SpawnPoint at random
var prefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
var point = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(prefab, point.position, point.rotation);
yield return new WaitForSeconds(spawnInterval);
}
yield return new WaitForSeconds(waveCooldown);
}
}
}📌 Quick recap
- ▸Prefab = reusable template — design once, use forever
- ▸An Instance can Override individual properties
- ▸Edit the Prefab asset → every linked Instance updates
- ▸High-frequency spawn → use Object Pooling instead of Instantiate/Destroy
⚠️ Common mistakes
❌ Instantiate without keeping the reference
You spawn it but cannot control that clone later
✅ var clone = Instantiate(prefab);
❌ Instantiate/Destroy every frame in a hot loop
GC spikes and hitching — especially machine-gun bullets
✅ Object Pool: pre-create, enable/disable instead of create/destroy