Creating a Prefab
- Game Development course
- Intermediate
- Lesson 50/83
- 4 min
Learn to make a prefab by dragging from Hierarchy to Project.
Creating a Prefab from the Hierarchy
To turn a GameObject into a Prefab, drag it from the Hierarchy window into a folder in the Project window. Unity creates a Prefab asset as a result. The asset is stored in the Project window and can be used in different scenes.
// Hierarchy → Project
// GameObject → Prefab AssetChoosing a Prefab Asset Location
Choosing an appropriate folder in the Project window when creating Prefabs keeps the project organized. A folder named Prefabs is commonly created under Assets. Storing different object groups in separate folders makes assets easier to find as the project grows.
Assets/
Prefabs/
Enemies/
Props/
Items/Practice Load a Prefab Asset
Load the Prefab asset named Crate from the Resources folder using Resources.Load. Store the result in a GameObject variable and print a message if loading succeeds.
Starter code
using UnityEngine;
public class PrefabLoader : MonoBehaviour
{
void Start()
{
}
}Sample solution
using UnityEngine;
public class PrefabLoader : MonoBehaviour
{
void Start()
{
GameObject cratePrefab = Resources.Load<GameObject>("Crate");
if (cratePrefab != null)
{
Debug.Log("Crate Prefab yüklendi");
}
}
}Expected output
Crate Prefab yüklendi
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which action saves an object from the Hierarchy as a Prefab?
- Delete the object's Transform component
- Change the Game window resolution
- Drag the object into a folder in the Project window Correct answer
- Move the object into the Console window
Explanation: A Prefab asset is created when the object in the Hierarchy is dragged into the Project window.
2 Complete the Prefab Folder Path
Prefab kaynaklarını düzenli saklamak için genellikle Assets klasörü altında ____ adlı bir klasör oluşturulur.
Prefab kaynaklarını düzenli saklamak için genellikle Assets klasörü altında Prefabs adlı bir klasör oluşturulur.
Explanation: The Prefabs folder is used to keep Prefab assets together.
3 Match the Prefab Creation Process
| Term | Meaning |
|---|---|
Hierarchy |
Displays GameObjects in the scene |
Project |
Displays project assets and folders |
Drag and Drop |
Moves an object by dragging it |
Prefab Asset |
The saved reusable object template |