0
0
Unityframework~10 mins

Instantiating objects at runtime in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Instantiating objects at runtime
Start
Load Prefab
Call Instantiate()
New Object Created
Set Position/Rotation
Object Active in Scene
End
The program loads a prefab, calls Instantiate to create a new object, sets its position and rotation, then the object appears active in the scene.
Execution Sample
Unity
public GameObject prefab;

void Start() {
  GameObject obj = Instantiate(prefab, new Vector3(0,0,0), Quaternion.identity);
}
This code creates a new instance of the prefab at position (0,0,0) with no rotation when the game starts.
Execution Table
StepActionPrefab StateNew Object StateOutput
1Start method calledPrefab loadedNo new objectNo output
2Instantiate calledPrefab loadedNew object created (active immediately)No output
3Set position to (0,0,0)Prefab loadedNew object position setNo output
4Set rotation to identityPrefab loadedNew object rotation setNo output
5New object active in scenePrefab loadedNew object active and visibleNo output
💡 Instantiation complete, new object is active in the scene.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
prefabLoaded prefab objectLoaded prefab objectLoaded prefab objectLoaded prefab objectLoaded prefab object
objnullNew GameObject instancePosition set to (0,0,0)Rotation set to identityActive in scene
Key Moments - 2 Insights
Why does the new object appear only after setting position and rotation?
Because Instantiate creates the object and it is active immediately, but the object appears correctly placed only after position and rotation are assigned, as shown in steps 2-5 in the execution_table.
Is the prefab itself changed when we instantiate a new object?
No, the prefab remains unchanged as seen in the prefab state column in the execution_table; only a new copy is created.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'obj' after step 3?
ANew object active and visible
BNo new object created yet
CNew GameObject instance with position set to (0,0,0)
DPrefab is modified
💡 Hint
Check the 'New Object State' column at step 3 in the execution_table.
At which step does the new object become active in the scene?
AStep 2
BStep 3
CStep 5
DStep 1
💡 Hint
Look for 'active' in the 'New Object State' column in the execution_table.
If we skip setting rotation, what changes in the execution_table?
APrefab would be modified
BStep 4 row would be missing or show no rotation set
CNew object would not be created
DObject would be active earlier
💡 Hint
Refer to the 'Action' column in the execution_table for step 4.
Concept Snapshot
Instantiate(prefab, position, rotation) creates a new object copy at runtime.
Prefab stays unchanged.
Set position and rotation to place the object.
New object becomes active and visible after setup.
Use in Start() or any method to spawn objects dynamically.
Full Transcript
This visual trace shows how Unity creates a new object from a prefab during runtime. First, the prefab is loaded. Then, Instantiate is called to make a new copy. The new object is active immediately. Next, its position and rotation are set to place it correctly in the scene. Finally, the object appears correctly placed and visible. The prefab itself does not change. Variables track the prefab and new object states step-by-step. Key moments clarify why the object appears only after setup and that the prefab remains unchanged. Quiz questions test understanding of object state at each step and effects of skipping steps.