TExcelTAD
Home › Unity with C# › The MonoBehaviour Lifecycle

The MonoBehaviour Lifecycle

You never call Update yourself. Unity's player loop calls a fixed set of "message" methods on your MonoBehaviours in a fixed order. Knowing that order answers most "why is this null?" and "why does this jitter?" questions.

The order

text
Object created (scene load or Instantiate)
  Awake()            once, even if the component is disabled (the GameObject must be active)
  OnEnable()         each time the component becomes enabled
  ...
Before the object's first frame
  Start()            once, only if enabled, after every Awake of the scene has run
Every frame
  FixedUpdate()      0, 1 or several times: fixed timestep (default 0.02 s), followed by the physics step
  OnTrigger*/OnCollision*   physics callbacks, after each physics step
  Update()           once
  (coroutines resume: yield return null)
  LateUpdate()       once, after every Update
  rendering
  (yield return new WaitForEndOfFrame resumes)
When disabled / destroyed
  OnDisable()
  OnDestroy()        Destroy() is deferred: it happens at the end of the current frame

The rule of thumb:

Method Use it for
Awake initialize this object: cache GetComponent results, create internal state
OnEnable / OnDisable subscribe to and unsubscribe from events (always in pairs)
Start anything that needs other objects to be initialized: they all ran Awake already
FixedUpdate physics: forces, Rigidbody velocities, MovePosition
Update input, timers, gameplay logic, non-physics movement
LateUpdate cameras that follow, things that react to where everything ended up this frame
OnDestroy release things you own: unsubscribe from static events, dispose native memory

A runnable player loop

The model below is a tiny engine. It runs the lifecycle for two components with a fixed-timestep accumulator (20 ms, like Unity's default Time.fixedDeltaTime), spawns a third object halfway through and destroys one. Frame times are integers in milliseconds, so the output is exact.

csharp · try it yourself
using System;
using System.Collections.Generic;

var loop = new PlayerLoop();
var player = loop.Add(new Logger("Player"));
var camera = loop.Add(new Logger("Camera"));
loop.StartScene();

int[] frameMs = { 16, 16, 33, 8 };
for (int f = 0; f < frameMs.Length; f++)
{
    if (f == 1) player.OnUpdate = () => loop.Instantiate(new Logger("Bullet"));
    else player.OnUpdate = null;
    if (f == 2) camera.OnUpdate = () => loop.Destroy(player);
    else camera.OnUpdate = null;
    loop.Frame(f + 1, frameMs[f]);
}

class Behaviour
{
    public string Name = "";
    public bool Started, PendingDestroy;
    public virtual void Awake() { } public virtual void OnEnable() { } public virtual void Start() { }
    public virtual void FixedUpdate() { } public virtual void Update() { } public virtual void LateUpdate() { }
    public virtual void OnDisable() { } public virtual void OnDestroy() { }
}

class Logger : Behaviour
{
    public Action? OnUpdate;
    public Logger(string name) => Name = name;
    void Log(string msg) => Console.WriteLine(
quot; {Name}.{msg}"); public override void Awake() => Log("Awake"); public override void OnEnable() => Log("OnEnable"); public override void Start() => Log("Start"); public override void FixedUpdate() => Log("FixedUpdate"); public override void Update() { Log("Update"); OnUpdate?.Invoke(); } public override void LateUpdate() => Log("LateUpdate"); public override void OnDisable() => Log("OnDisable"); public override void OnDestroy() => Log("OnDestroy"); } class PlayerLoop { const int FixedStepMs = 20; readonly List<Behaviour> objects = new(); int accumulatorMs; public T Add<T>(T b) where T : Behaviour { objects.Add(b); return b; } public void StartScene() { Console.WriteLine("scene load"); foreach (var b in objects) { b.Awake(); b.OnEnable(); } // Awake + OnEnable per object } public void Instantiate(Behaviour b) { Console.WriteLine(
quot; -> Instantiate {b.Name}"); b.Awake(); b.OnEnable(); // run immediately, inside the call objects.Add(b); } public void Destroy(Behaviour b) { Console.WriteLine(
quot; -> Destroy {b.Name} (deferred)"); b.PendingDestroy = true; } public void Frame(int n, int ms) { Console.WriteLine(
quot;frame {n} ({ms} ms)"); var snapshot = new List<Behaviour>(objects); foreach (var b in snapshot) if (!b.Started) { b.Start(); b.Started = true; } accumulatorMs += ms; while (accumulatorMs >= FixedStepMs) { foreach (var b in snapshot) b.FixedUpdate(); Console.WriteLine(" [physics step]"); accumulatorMs -= FixedStepMs; } foreach (var b in snapshot) b.Update(); foreach (var b in snapshot) b.LateUpdate(); Console.WriteLine(" [render]"); foreach (var b in objects.FindAll(o => o.PendingDestroy)) // end of frame { b.OnDisable(); b.OnDestroy(); objects.Remove(b); } } }
scene load
  Player.Awake
  Player.OnEnable
  Camera.Awake
  Camera.OnEnable
frame 1 (16 ms)
  Player.Start
  Camera.Start
  Player.Update
  Camera.Update
  Player.LateUpdate
  Camera.LateUpdate
  [render]
frame 2 (16 ms)
  Player.FixedUpdate
  Camera.FixedUpdate
  [physics step]
  Player.Update
  -> Instantiate Bullet
  Bullet.Awake
  Bullet.OnEnable
  Camera.Update
  Player.LateUpdate
  Camera.LateUpdate
  [render]
frame 3 (33 ms)
  Bullet.Start
  Player.FixedUpdate
  Camera.FixedUpdate
  Bullet.FixedUpdate
  [physics step]
  Player.FixedUpdate
  Camera.FixedUpdate
  Bullet.FixedUpdate
  [physics step]
  Player.Update
  Camera.Update
  -> Destroy Player (deferred)
  Bullet.Update
  Player.LateUpdate
  Camera.LateUpdate
  Bullet.LateUpdate
  [render]
  Player.OnDisable
  Player.OnDestroy
frame 4 (8 ms)
  Camera.Update
  Bullet.Update
  Camera.LateUpdate
  Bullet.LateUpdate
  [render]

Things to notice:

  1. Frame 1 has no FixedUpdate: only 16 ms had accumulated. Frame 3 (33 ms) runs it twice. FixedUpdate is not "every frame", which is why reading input there misses presses.
  2. Awake runs inside Instantiate, so the code right after Instantiate can rely on it. Start waits until the next frame.
  3. Destroy is deferred: the Player still got its LateUpdate in frame 3 after being destroyed, and only disappeared at the end of the frame.

Try changing the frame times to { 50, 5, 5, 5 } and predict the output before running it.

Unity code using the lifecycle

csharp
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody))]
public class Ball : MonoBehaviour
{
    [SerializeField] float pushForce = 10f;
    Rigidbody body;
    Vector2 moveInput;                         // read in Update, used in FixedUpdate
    InputAction moveAction;

    void Awake()
    {
        body = GetComponent<Rigidbody>();                  // own components: Awake
        moveAction = InputSystem.actions.FindAction("Move");
    }

    void Start()
    {
        var spawn = FindFirstObjectByType<SpawnPoint>();   // other objects: Start (and only once!)
        if (spawn != null) body.position = spawn.transform.position;
    }

    void Update()
    {
        moveInput = moveAction.ReadValue<Vector2>();       // input: every frame
    }

    void FixedUpdate()
    {
        var force = new Vector3(moveInput.x, 0f, moveInput.y) * pushForce;
        body.AddForce(force, ForceMode.Force);             // physics: fixed step
    }
}

Script execution order between different scripts is undefined unless you set it. If one script's Awake must run before another's, prefer moving the dependent code to Start over editing Project Settings > Script Execution Order or using [DefaultExecutionOrder(-100)]; both work but hide coupling.

Test yourself

Which method runs even when the component starts disabled, as long as its GameObject is active?

  1. Start
  2. Awake
  3. Update
  4. OnEnable

At 144 FPS with the default fixed timestep (0.02 s), how often does FixedUpdate run?

  1. Every frame
  2. About 50 times per second, so most frames have none
  3. 144 times per second
  4. Twice per frame

You call Destroy(gameObject) in Update. When is the object actually removed?

  1. Immediately
  2. At the end of the current frame
  3. After the next FixedUpdate
  4. When the scene unloads