In this lesson, you will learn how to shift your game logic away from the Update method to minimize CPU overhead and keep your frame rates consistent.
The Update method runs every single frame—often 60 to 144 times per second. When you place heavy calculations, complex logic, or repeated state checks here, you consume valuable milliseconds that could otherwise be used for rendering or physics. In advanced performance tuning, the goal is to stop "polling" (constantly checking) and start "reacting."
From Polling to Events
Instead of checking if (isGameOver) in Update, use an event-driven pattern. By subscribing to a C# Action or a UnityEvent, your script sits idle until the specific condition is met, at which point the logic executes exactly once.
// Inefficient: Checking every frame
void Update() {
if (health <= 0) Die();
}
// Efficient: Event-driven
void OnEnable() => GameEvents.OnHealthDepleted += Die;
void OnDisable() => GameEvents.OnHealthDepleted -= Die;
Time-Slicing Logic
If you have a task that must run periodically but doesn't need to happen every frame, use Time-Slicing. Instead of executing it in Update, wrap it in a Coroutine or a timer that throttles the execution frequency.
private float _timer;
private const float Interval = 0.5f; // Runs twice per second instead of 60+
void Update() {
_timer += Time.deltaTime;
if (_timer >= Interval) {
PerformHeavyCalculation();
_timer = 0;
}
}
By moving logic into Coroutines with WaitForSeconds or setting up custom timers, you break the "frame-dependency" of your code. This spreads the computational load across multiple frames, preventing sudden CPU spikes that cause stuttering (dropped frames). Whenever you find yourself writing logic in Update, ask: "Does this really need to happen every 16 milliseconds, or can I trigger it via an event or a less frequent interval?"
Key Takeaway: Never execute logic every frame if you can trigger it via an event or distribute it across multiple frames using time-slicing.