What is a Prefab?
- Game Development course
- Intermediate
- Lesson 49/83
- 4 min
Learn that a Prefab is a reusable GameObject template.
What Is a Prefab?
A Prefab is a reusable template of a GameObject, stored together with its components and settings. Once you save an object as a Prefab, you can reuse the same structure in different scenes. For example, you can create a Prefab for an enemy, a chest, or a decorative object.
// Enemy Prefab
// Chest Prefab
// Decoration PrefabManaging Changes with Prefabs
Scene instances connected to a Prefab asset can be affected by changes made to the source. For example, when you edit a shared component in the Prefab, the change can be applied to connected instances. This allows you to manage similar objects from a single source.
// Shared Prefab
// Multiple Instances
// Centralized ChangesPractice Define a Prefab Reference
Declare a public variable in a Unity C# script that can hold a GameObject Prefab asset. A Prefab can then be assigned to this variable through the Inspector.
Starter code
using UnityEngine;
public class PrefabReference : MonoBehaviour
{
}Sample solution
using UnityEngine;
public class PrefabReference : MonoBehaviour
{
public GameObject objectPrefab;
}Expected output
objectPrefab değişkeni tanımlandı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the main advantage of using Prefabs?
- Automatically turning off all lights in the scene
- Running code files without compiling
- Changing the game resolution
- Being able to reuse the same GameObject structure Correct answer
Explanation: A Prefab allows you to reuse the same object structure in different places.
2 Complete the Purpose of a Prefab
Prefab, bir GameObject'i yeniden kullanılabilir bir ____ olarak saklamanı sağlar.
Prefab, bir GameObject'i yeniden kullanılabilir bir şablon olarak saklamanı sağlar.
Explanation: A Prefab stores a GameObject structure as a reusable template.
3 Match Prefab Usage
| Term | Meaning |
|---|---|
Prefab |
A reusable GameObject template |
GameObject |
An object in a scene |
Reuse |
Using the same structure again |
Prefab Asset |
A Prefab asset stored in the Project |