Tier
Series 1
1D: Efficiency Audit
Pre-Flight
"Checking your oil is a Start procedure. You do it once before the flight. Checking your altitude is an Update procedure. You do it constantly while in the air. If a pilot tries to check the oil 60 times every second while flying, the plane will eventually fail due to the unnecessary 'drag' on the pilot's attention."
Sanity Check
The Concept: Start() vs. Update()
As detailed in Chapter 2, Unity scripts follow a specific lifecycle. Knowing when an action happens is just as important as the action itself.
Start(): Runs once when the script begins. Perfect for "expensive" setup tasks like finding components.
Update(): Runs every frame (up to 60+ times per second). Used for movement and input.
Start(): Runs once when the script begins. Perfect for "expensive" setup tasks like finding components.
Update(): Runs every frame (up to 60+ times per second). Used for movement and input.
Concept Analysis
Audit Warning
The AI Trap: "The Performance Drag"
You ask the AI: "Write a script that makes a game object change color when I press a key."
// AI-Generated Code: Creating major lag!
void Update() {
// Audit Error: GetComponent is VERY expensive
// to run 60 times per second!
GetComponent<Renderer>().material.color = Color.red;
}
Reasoning: The Sanity Error: GetComponent is like opening the hood of the car to check the engine. Doing this 60 times a second will cause your game's frame rate to "stutter" or drop, especially on mobile devices.
Trap Breakdown
The Pilot's Command
Operational Protocol Corrected
Solution Logic
// Corrected Pilot Code: Smooth & Efficient
private Renderer myRenderer;
void Start() {
// Check the "oil" once at the beginning
myRenderer = GetComponent<Renderer>();
}
void Update() {
// Use the stored reference for high-speed flight
myRenderer.material.color = Color.red;
}