Tier 2
Series 103
103F: The Physics Query Audit
"A radar ping shouldn't require you to buy a new radar screen every time. But in Unity, some physics commands create a new "Array" (Screen) every single time they run. A skilled Engineer audits for Non-Allocating Queries."
The Concept: NonAlloc Methods
Standard physics queries like `OverlapSphere` create a new array of colliders every time they are called. This creates "Garbage" in memory.
* **OverlapSphere:** Creates garbage. Bad for frequent use.
* **OverlapSphereNonAlloc:** Reuses the same array. Zero garbage. Professional standard.
* **OverlapSphere:** Creates garbage. Bad for frequent use.
* **OverlapSphereNonAlloc:** Reuses the same array. Zero garbage. Professional standard.
Red Flag Detected
The AI Trap: "The Memory Spike"
You ask the AI: "Find all enemies in an explosion radius."
// AI-Generated Code: Garbage Generator
void Update() {
// Audit Fail: Creates a NEW array [] 60 times a second.
Collider[] hits = Physics.OverlapSphere(pos, 5f);
}
This is "Garbage Generation." Doing this every frame causes the Garbage Collector to stutter the game frame rate.
Elite Telemetry
Research shows "Elite" teams achieve 15% faster lead times by keeping AI on a "very tight leash."
- Small Batches Solving one problem at a time prevents logic drift.
- Modular Design Localizing the "blast radius" of AI changes.
- Tight Loops Rapid iteration with constant code review.
The Pilot's Correction
Corrective Protocol
// Corrected: Zero-Garbage
Collider[] resultsBuffer = new Collider[10]; // Reusable Screen
void Update() {
// Writes into the existing buffer. No new memory created.
int count = Physics.OverlapSphereNonAlloc(pos, 5f, resultsBuffer);
}
Your Pilot Command
> Capstone Mission: Audit the animation controller logic for state-machine efficiency.