0
0
Unityframework~10 mins

Prefabs and reusable objects in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prefabs and reusable objects
Create GameObject
Configure GameObject
Create Prefab from GameObject
Use Prefab in Scene
Modify Prefab
Prefab Updates All Instances
You create a game object, turn it into a prefab, then reuse it many times. Changing the prefab updates all copies.
Execution Sample
Unity
GameObject enemy = new GameObject("Enemy");
// Add components and set properties
PrefabUtility.SaveAsPrefabAsset(enemy, "Assets/Resources/Prefabs/Enemy.prefab");

// Later in scene
GameObject enemyInstance = Instantiate(Resources.Load<GameObject>("Prefabs/Enemy"));
Create an enemy object, save it as a prefab, then create instances from that prefab in the scene.
Execution Table
StepActionObject StatePrefab StateResult
1Create GameObject 'Enemy'Enemy created, no prefabNo prefab yetGameObject exists in scene
2Configure Enemy (add components)Enemy has componentsNo prefab yetGameObject ready for prefab
3Save as prefab 'Enemy.prefab'Enemy linked to prefabPrefab saved with Enemy dataPrefab asset created
4Instantiate prefab in sceneNew Enemy instance createdPrefab unchangedInstance appears in scene
5Modify prefab assetPrefab updatedPrefab asset changedAll instances update automatically
6Modify instance onlyInstance changedPrefab unchangedInstance overrides prefab settings
7Exit--No more actions
💡 No more actions to perform; prefab and instances are set up.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5Final
enemy (GameObject)nullCreated (no prefab)Linked to prefabInstance createdPrefab updatedPrefab and instances synced
prefab (Asset)nullnullCreated with enemy dataUnchangedUpdatedUpdated prefab asset
Key Moments - 3 Insights
Why does changing the prefab update all instances automatically?
Because all instances are linked to the prefab asset, so when the prefab changes (see step 5 in execution_table), Unity updates all instances to match.
What happens if you change only one instance and not the prefab?
The instance overrides the prefab settings locally (step 6), so that instance looks different but the prefab and other instances stay the same.
Why do we create a prefab instead of just copying GameObjects?
Prefabs let you reuse objects easily and update all copies at once, unlike copies which are independent and need manual updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the prefab asset first created?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Prefab State' column in execution_table rows.
According to variable_tracker, what is the state of 'enemy' after step 4?
ACreated but no prefab
BLinked to prefab
CInstance created
DPrefab updated
💡 Hint
Look at the 'enemy (GameObject)' row under 'After Step 4' in variable_tracker.
If you modify only one instance (step 6), what happens to the prefab asset?
APrefab asset remains unchanged
BPrefab asset is deleted
CPrefab asset updates
DPrefab asset duplicates
💡 Hint
See 'Prefab State' column in execution_table at step 6.
Concept Snapshot
Prefabs are reusable game object templates.
Create a GameObject, configure it, then save as prefab.
Instantiate prefab copies in scenes.
Changing prefab updates all instances.
Instance changes override prefab locally.
Full Transcript
Prefabs in Unity let you create a game object once and reuse it many times. First, you create and set up a GameObject, then save it as a prefab asset. This prefab acts like a template. When you add prefab instances to your scene, they link back to the prefab. If you change the prefab asset, all instances update automatically. But if you change only one instance, it overrides the prefab locally without affecting others. This system helps keep your game organized and easy to update.