←All patterns
Game Programming

Update Method

Give every entity a per-frame method and let the game loop call it, so each object simulates one frame of its own behaviour.

What Is It?

A game world is full of things that act independently: coins bob, turrets turn, projectiles fly, timers count down. The Update Method pattern gives each of them a method that does one frame of work, and a loop that calls that method on every live object once per frame. Nobody has to know what the others are doing.

Unity is built on this idea. Every MonoBehaviour with an Update() is registered with the engine and called each frame. That is convenient, but each of those calls crosses from native engine code into managed C#, and with thousands of tiny components that per-call overhead (plus the engine iterating and null-checking each behaviour) adds up before any of your logic runs.

The game-programming version makes the list explicit: an IUpdatable interface with Tick(float dt), and one UpdateManager whose single Update() walks a List<IUpdatable>. One native-to-managed call per frame instead of one per object, a defined order, and an obvious place to pause, slow down or profile a whole group of entities.

When Is It Used?

Use it whenever a set of objects each need small, independent per-frame behaviour: ambient props, pickups, projectiles, AI agents that think a little every frame, cooldown timers.

Switch from per-object Update() to a manager when the profiler shows many hundreds of tiny Update calls, when you need a deterministic order between entities, or when you want to pause, time-scale or LOD a whole category (for example, tick far-away enemies every fourth frame).

Do not use it for things that only react to events, or that change rarely. A door that opens once should not be asked "anything to do?" sixty times a second; let an event or a coroutine wake it up.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

Update Method Pattern
entities1000engine calls1000overhead1.20 msframe0
Unity
player loop
UpdateManager
not used
pendingAdd
#21
#22
#23
#24
#25
#26
#27
#28
#29
#30
#31
#32
#33
#34
#35
#36
#37
#38
#39
#40
each tile = 50 entities
Console
Press a button to see what happens…

Code

Assets / Scripts / Game Programming/ UpdateMethod ›SpinnerSpawner.cs
using UnityEngine;

namespace Patterns.GameProgramming.UpdateMethod
{
    /// <summary>
    /// Entry point: fills the scene with spinners. None of them owns an
    /// Update(); a single UpdateManager ticks them all.
    /// </summary>
    public class SpinnerSpawner : MonoBehaviour
    {
        [SerializeField] private Spinner prefab;
        [SerializeField] private int count = 1000;
        [SerializeField] private float radius = 20f;

        private void Start()
        {
            for (int i = 0; i < count; i++)
            {
                Vector2 p = Random.insideUnitCircle * radius;
                Instantiate(prefab, new Vector3(p.x, p.y, 0f), Quaternion.identity, transform);
            }

            Debug.Log($"Spawned {count} spinners, {UpdateManager.Instance.Count} registered for Tick");
        }

        private void Update()
        {
            // Spawning from inside a frame is fine too: Register is deferred while ticking.
            if (Input.GetKeyDown(KeyCode.Space))
                Instantiate(prefab, transform.position, Quaternion.identity, transform);
        }
    }
}
4 files · namespace Patterns.Game Programming.UpdateMethodC# · UTF-8 · LF

Before & After

Real-world smell: eight hundred coins that each own an update callback. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

Coin has its own Update() that bobs and spins the coin. The level has 800 of them, so Unity makes 800 separate native-to-managed calls every frame for a few lines of math each.

In the profiler this shows up as a long list of tiny Coin.Update entries plus engine overhead around them. There is also no single place to pause the coins during a cutscene or to stop ticking the ones far from the camera.

✓ What changed

  • Coin now implements IUpdatable, and its old Update() body moved unchanged into Tick(float dt).
  • The coin registers with UpdateManager.Instance in OnEnable and unregisters in OnDisable, so collecting it with Destroy stops its ticks automatically.
  • Because UpdateManager defers removals during a tick, calling Collect() from inside another entity’s Tick cannot corrupt the list.
Coin.Before.cs → Coin.After.cs+12/−5
11 using UnityEngine;
22
33 namespace Patterns.GameProgramming.UpdateMethod
44 {
55 /// <summary>
6− /// Without a central tick: each of the 800 coins in the level owns a
7− /// Unity Update() just to bob and spin a little.
6+ /// With Update Method: the coin implements Tick and lets one
7+ /// UpdateManager drive all 800 coins from a single Update().
88 /// </summary>
9− public class Coin : MonoBehaviour
9+ public class Coin : MonoBehaviour, IUpdatable
1010 {
1111 [SerializeField] private float spinSpeed = 90f;
1212 [SerializeField] private float bobHeight = 0.25f;
1313 [SerializeField] private float bobSpeed = 2f;
1414
1515 private Vector3 origin;
1616 private float phase;
1717
1818 private void Awake()
1919 {
2020 origin = transform.localPosition;
2121 phase = Random.value * Mathf.PI * 2f;
2222 }
2323
24− private void Update()
24+ private void OnEnable() => UpdateManager.Instance.Register(this);
25+
26+ private void OnDisable()
2527 {
26− float dt = Time.deltaTime;
28+ if (UpdateManager.Instance != null)
29+ UpdateManager.Instance.Unregister(this);
30+ }
2731
32+ public void Tick(float dt)
33+ {
2834 phase += bobSpeed * dt;
2935 transform.localPosition = origin + Vector3.up * (Mathf.Sin(phase) * bobHeight);
3036 transform.Rotate(0f, spinSpeed * dt, 0f);
3137 }
3238
3339 public void Collect()
3440 {
41+ // OnDisable unregisters; safe even if this runs inside another Tick.
3542 Destroy(gameObject);
3643 }
3744 }
3845 }

Advantages & Disadvantages

+ Advantages

  • Each entity owns its own behaviour, so adding a new kind of object never touches a central switch statement.
  • A manager replaces thousands of engine callbacks with one, and gives you a single place to profile, pause or throttle updates.
  • Iteration order becomes explicit and controllable instead of depending on Script Execution Order settings.

− Disadvantages

  • Adding or removing entities while iterating the list skips items, ticks something twice or throws InvalidOperationException. You need deferred add/remove.
  • Every entity is polled every frame even when it has nothing to do, which wastes time on idle objects.
  • A hand-rolled manager must mirror enable/disable correctly; a disabled object that forgets to unregister keeps ticking or holds a dead reference.

Tips

  1. 01Register in OnEnable and unregister in OnDisable so SetActive(false) and Destroy both stop the ticks automatically.
  2. 02Never mutate the list inside the loop. Queue adds and removals in pendingAdd/pendingRemove and apply them after the pass, or iterate a copy if the list is small.
  3. 03Pass Time.deltaTime into Tick so every entity uses the same value and you can feed a scaled or paused delta for a whole group.
  4. 04Empty Update() methods still cost a call each frame. Delete them, including the ones Unity’s script template creates.
  5. 05Measure first. A manager pays off with hundreds or thousands of small updaters; for twenty objects, plain Update() is clearer and just as fast.