Scenes and prefabs
Organise a scene hierarchy and reuse objects with prefab assets, variants and runtime instantiation.
- Turn any repeated GameObject into a prefab.
- Use prefab variants to share a base while varying details.
- Instantiate prefabs from a script at runtime.
In plain English
A scene hierarchy is a tree of GameObjects, and the parent and child relationship lets you move and organise whole groups at once. A prefab is a reusable asset that stores a GameObject with its components and settings, so you edit it once and every copy updates. Instantiating means creating a copy of a prefab at runtime, usually from a script, and a prefab variant is a child prefab that inherits from a base prefab while overriding a few values. Prefabs matter because they keep repeated objects such as enemies and pickups consistent and easy to change.
Step by step
- 1Create a GameObject and add the components it needs.
- 2Drag it into the Project window to create a prefab asset.
- 3Create a prefab variant and override one property.
- 4Write a script that instantiates the prefab at runtime.
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private GameObject enemyPrefab;
private void Start()
{
Instantiate(enemyPrefab, transform.position, Quaternion.identity);
}
}Common mistakes
- Editing a copy in the scene only changes that copy; to update every copy you must edit the prefab asset itself.
- Overriding a property on an instance breaks its link to the prefab for that property, so later prefab edits will not reach it.
- Instantiating in Update without a limit can create hundreds of objects per frame and freeze the game.
- Leaving many unused objects as separate scene copies instead of prefabs makes future changes slow and error-prone.
Spawn a prefab
Write a Spawner that instantiates an enemy prefab at its own position when the scene starts.
using UnityEngine;
public class Spawner : MonoBehaviour
{
[SerializeField] private GameObject enemyPrefab;
private void Start()
{
}
}You edit a prefab asset after placing several instances. What happens?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.