Manual: Order of execution for event functions (original) (raw)

Inspector-configurable custom events

Event functions are a set of built-in events that your MonoBehaviour scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary can optionally subscribe to by implementing the appropriate methods, often referred to as callbacks. The callbacks correspond to events in core Unity subsystems like physics, rendering, and user input, or to stages of the script’s own lifecycle such as its creation, activation, frame-dependent and frame-independent updates, and destruction. When the event occurs, Unity invokes the associated callback on your script, giving you the opportunity to implement logic in response to the event.

To the extent that Unity raises these events and calls the associated MonoBehaviour callbacks in a predetermined order, the order is documented here. It’s important to understand the execution order so you don’t try to use one callback to do work which depends on another callback that hasn’t been invoked yet. However, bear in mind that some callbacks are for events, such as those triggered by user inputs, which can occur at any time while your game is running. You should consult this page in combination with the MonoBehaviour script reference (where the event callbacks are listed under Messages) for a complete understanding of each event’s meaning and limitations.

Script lifecycle overview

The diagram below summarizes how Unity orders and repeats event functions over a script’s lifetime.

For more information about the various event functions, see the following sections:

Scope of the flowchart

The scope of the flowchart below is limited to the built-in event functions that you can subscribe to on any MonoBehaviour script by implementing the appropriate callbacks documented under Messages in the MonoBehaviour scripting reference. Some additional internal methods local to the subsystems that raise the events are also shown for context.

In addition to these built-in event functions there are a number of other events you can potentially subscribe to in your scripts. Several major classes such as Application, SceneManager, and CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary offer delegates that you can register your own callback methods with. Method attributes like RuntimeInitializeOnLoadMethodAttribute can also be used to execute methods at certain stages of the sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary. Refer to the scripting reference for the component or subsystem you’re interested in to see what event callbacks you can subscribe to and details of their execution order.

Script lifecycle flowchart

Order of execution for event functions during the lifecycle of a MonoBehaviour script.

Order of execution for event functions during the lifecycle of a MonoBehaviour script.

Note: Some browsers do not support SVG image files. If the image above does not display properly (for example, if you cannot see any text), please try another browser, such as Google Chrome or Mozilla Firefox.

General principles

In general, you should not rely on the order in which the same event function is invoked for different GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary — except when the order is explicitly documented or settable. If you need a more fine-grained control of the player loop, you can use the PlayerLoop API.
You cannot specify the order in which an event function is called for different instances of the same MonoBehaviour subclass. For example, the Update function of one MonoBehaviour might be called before or after the Update function for the same MonoBehaviour on another GameObject — including its own parent or child GameObjects.

You can specify that the event functions of one MonoBehaviour subclass should be invoked before those of a different subclass using the Script Execution Order panel of the Project SettingsA broad collection of settings which allow you to configure how Physics, Audio, Networking, Graphics, Input and many other areas of your project behave. More info
See in Glossary window. For example, if you had two scripts, EngineBehaviour and SteeringBehaviour, you could set the Script Execution Order such that EngineBehaviours always update before SteeringBehaviours. If loading multiple scenes additively, the configured script execution order is applied in full one scene at a time, rather than partially across scenes, so EngineBehaviours and SteeringBehaviours would both update on one scene before they updated on the next one.

First Scene load

These functions get called when a scene starts (once for each object in the scene).

For objects that are part of a scene asset, Awake and OnEnable functions for all scripts are called before Start and subsequent functions are called for any of them. However, this can’t be enforced when you instantiate an object at runtime.

Awake is only guaranteed to be called before OnEnable in the scope of each individual object. Across multiple objects the order is not deterministic and you can’t rely on one object’s Awake being called before another object’s OnEnable. Any work that depends on Awake having been called for all objects in the scene should be done in Start.

Before scene load and unload

Not shown in the diagram above are the SceneManager.sceneLoaded and SceneManager.sceneUnloaded events which allow you to receive callbacks when a scene has loaded and unloaded respectively. Refer to the relevant scripting reference pages for details and example usage. You can expect to receive the sceneLoaded notification after OnEnable but before Start for all objects in the scene. Refer to Details of disabling Domain and Scene reload for a diagram that includes scene load as part of the execution flow.

You can also use the RuntimeInitializeOnLoadMethodAttribute and its types BeforeSceneLoad and AfterSceneLoad to make your methods run before or after scene load respectively. Refer to the RuntimeInitializeOnLoadMethodAttribute scripting reference main page for execution order information for methods marked with these types.

Editor

Before the first frame update

For objects that are part of a scene asset, the Start function is called on all scripts before Update is called for any of them. However, this cannot be enforced when you instantiate an object during gameplay. For example, if you instantiate an object from another object’s Update function, the instantiated object’s Start can’t be called until Update runs for the first time on the original object.

In between frames

Update Order

When you’re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update function, but there are also other functions you can use.

Animation update loop

The following Animation loop callbacks shown in the flowchart above are called on scripts that derive from MonoBehaviour:

Additional animation-related event functions are called on scripts that derive from StateMachineBehaviour:

For the meaning and limitations of these callbacks, refer to the relevant scripting reference pages.

Other animation functions shown in the flowchart are internal to the animation system and are provided for context. These functions have associated Profiler markers so you can use the ProfilerA window that helps you to optimize your game. It shows how much time is spent in the various areas of your game. For example, it can report the percentage of time spent rendering, animating, or in your game logic. More info
See in Glossary to see when in the frame Unity calls them. Knowing when Unity calls these functions can help you understand exactly when the Event functions you do call are executed. For a full execution order of animation functions and profiler markers, refer to Profiler markersPlaced in code to describe a CPU or GPU event that is then displayed in the Unity Profiler window. Added to Unity code by default, or you can use ProfilerMarker API to add your own custom markers. More info
See in Glossary.

Rendering

This execution order applies for the Built-in Render Pipeline only. For details of execution order in render pipelinesA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary based on the Scriptable Render Pipeline, refer to the relevant sections of the documentation for the Universal Render Pipeline or the High Definition Render Pipeline. If you want to do work immediately prior to rendering, refer to Application.onBeforeRender.

Note: OnPreCull, OnPreRender, OnPostRender, and OnRenderImage are built-in Unity event functions that are called on MonoBehaviour scripts but only if those scripts are attached to the same object as an enabled Camera component. If you want to receive the equivalent callbacks for OnPreCull, OnPreRender, and OnPostRender on a MonoBehaviour attached to a different object, you must use the equivalent delegates (note the lowercase on in the names) Camera.onPreCull, Camera.onPreRender, and Camera.onPostRender as shown in the code examples in the relevant pages of the scripting reference.

Coroutines

Normal coroutine updates are run after the Update function returns. A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes.

Different uses of coroutines:

When the Object is destroyed

When quitting

These functions get called on all the active objects in your scene:

Additional resources

Inspector-configurable custom events