Tier 1 Series 2

2E: Value vs. Reference

Pilot Record
Student Profile
"Imagine a pilot carrying a small, heavy tool in their flight-suit pocket. It is always within reach (Stack). Now imagine checking a massive trunk into the aircraft's cargo hold. To get it back, you need ground crew and baggage handlers (Garbage Collector) to find it (Heap).

A skilled Pilot audits their data containers to ensure they aren't accidentally putting "pocket change" into a "cargo trunk.""

The Concept: Memory Storage

Choosing the wrong type forces Unity to perform "Boxing"—a major cause of frame-rate stutter.

The Stack (Value): Ultra-fast. Stores int, float, bool, struct.
The Heap (Reference): Slower. Stores class, string, Array.
Boxing: The expensive process of wrapping a Value Type in a "Box" to store it on the Heap.
Red Flag Detected

The Audit: Spot the "Boxing" Error

You ask the AI: "Write a list to store a collection of different whole numbers for my score manager."

// AI-Generated Code (Memory Heavy)
public List<object> scores = new List<object>();

void AddScore(int value) {
    scores.Add(value); // ERROR: Boxing occurs here!
}

By using List<object>, you are forcing every int (a Value Type) to be "boxed" into an object (a Reference Type). This creates trash on the Heap that the Garbage Collector must eventually stop the game to clean up.

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 Code (Type Safe & Fast)
public List<int> scores = new List<int>();

void AddScore(int value) {
    scores.Add(value); // Success: Stays in the pocket!
}
Your Pilot Command
> Use TryGetComponent to safely handle null checks and component retrieval.
Next Mission
Static Utilities