Summary - Prefab
- Game Development course
- Intermediate
- Lesson 54/83
- 4 min
Reinforce prefab usage with a summary task.
Summarize the Prefab Workflow
A Prefab is a reusable GameObject template. You can create a source asset by dragging an object from the Hierarchy into the Project window. Adding this asset to a scene creates an instance. The Instantiate method creates new instances at runtime. Prefab Variants allow you to create customized derivatives connected to the base source.
// Create → Store → Reuse
// Scene Instance
// Runtime Instantiate
// Prefab VariantPractice Complete the Prefab Spawning System
Use a Prefab asset and spawn position assigned through the Inspector. In the SpawnCrate method, create a new object at the specified position with the default rotation if the asset is valid. Print the name of the created object to the console.
Starter code
using UnityEngine;
public class CrateSpawner : MonoBehaviour
{
public GameObject cratePrefab;
public Vector3 cratePosition;
// SpawnCrate metodunu buraya ekle
}Sample solution
using UnityEngine;
public class CrateSpawner : MonoBehaviour
{
public GameObject cratePrefab;
public Vector3 cratePosition;
public void SpawnCrate()
{
if (cratePrefab != null)
{
GameObject newCrate = Instantiate(cratePrefab, cratePosition, Quaternion.identity);
Debug.Log("Oluşturulan sandık: " + newCrate.name);
}
}
}Expected output
Oluşturulan sandık: Crate
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which structure is suitable for creating many similar enemies both in the scene and during gameplay?
- Converting all enemies into a single material
- Creating a new Animation Clip for every spawn
- Using a Prefab asset and Instantiate when needed Correct answer
- Opening a separate Unity project for every enemy
Explanation: A Prefab asset stores the shared object structure; Instantiate creates the required instances at runtime.
2 Recall the Runtime Creation Method
Bir Prefab kaynağından oyun çalışırken yeni bir nesne örneği üretmek için ____ metodu kullanılır.
Bir Prefab kaynağından oyun çalışırken yeni bir nesne örneği üretmek için Instantiate metodu kullanılır.
Explanation: Instantiate creates a new object instance from a Prefab asset at runtime.
3 Match Prefab Concepts
| Term | Meaning |
|---|---|
Prefab |
A reusable object template |
Prefab Asset |
The source stored in the Project window |
Instance |
A Prefab example in the scene |
Prefab Variant |
Custom derivative from the base source |