Unity script lifecycle
↑Quick overview
Section titled “Quick overview”Unity calls certain MonoBehaviour methods at specific points in a frame or a component’s lifetime. The most important choice is which work belongs in which phase. Incorrect placement can cause jitter, missed initialization, or input that feels delayed.
| Method | Typical responsibility | Important detail |
|---|---|---|
Awake |
Cache references and initialize local state | Called as the script instance is loaded, including for inactive scene objects in some circumstances when activated |
OnEnable |
Subscribe to events | Can run more than once |
Start |
Setup that depends on other objects being initialized | Called before the first Update when enabled |
Update |
Poll frame-based input and gameplay logic | Frame-rate dependent |
FixedUpdate |
Apply forces and physics-related changes | Fixed timestep; may run zero or multiple times per rendered frame |
LateUpdate |
Follow cameras or logic that should run after Update |
Runs after normal Update calls |
OnDisable |
Remove event subscriptions | Can occur when disabling a script or GameObject |
Work through a player controller
Section titled “Work through a player controller”- Cache the
RigidbodyinAwake, before doing gameplay work. - Read user input in
Updateso frame events are not skipped. - Apply physics forces or Rigidbody movement in
FixedUpdate, using the latest stored input. - Move a non-physics follow camera in
LateUpdateafter the player has updated. - Unsubscribe from events in
OnDisableto avoid duplicated callbacks.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]public sealed class PlayerMovement : MonoBehaviour{ private Rigidbody body; private Vector2 movement;
private void Awake() => body = GetComponent<Rigidbody>();
private void Update() { movement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); }
private void FixedUpdate() { Vector3 force = new(movement.x, 0f, movement.y); body.AddForce(force * 8f, ForceMode.Acceleration); }}This sample uses Unity’s legacy Input Manager to keep the lifecycle concept short. Projects using the new Input System collect input through the relevant actions instead.
Why it matters
Section titled “Why it matters”A camera following in Update can appear to trail a moving character if both scripts update in an inconvenient order. Moving the camera to LateUpdate helps, but using physics interpolation may also be necessary when the target moves through FixedUpdate. Diagnose the actual update source rather than moving everything to LateUpdate.
See also: Rigidbody and physics and Collisions and triggers.
Explore the lifecycle
Section titled “Explore the lifecycle”INTERACTIVE LIFECYCLE
Select a method to see when and why it is used. This simplified diagram does not imply a fixed one-to-one order between FixedUpdate and Update.
Awake
Set up fields and cache references belonging to this component.
Why it mattersAwake is used for local initialization, not assumptions about the order of other GameObjects.
