The following diagram provides a high-level overview of the execution sequence for event functions that run during the lifecycle of a MonoBehaviour script component. For readability, the scope of the chart is limited to key parts of the script lifecycle, with some extra internal subsystem updates provided for context. The full Player loop is a longer and more complex sequence of updates for specific systems and subsystems, which run one after the other in a defined default order. To retrieve the full Player loop and all of its systems, you can use the PlayerLoop API. You can also use this API to customize the Player loop sequence by removing systems, adding your own, and changing the update order.
For more information on each individual callback’s meaning and limitations, refer to the Messages section of the MonoBehaviour API reference.
Not shown in the previous diagram are the SceneManager.sceneLoaded
and SceneManager.sceneUnloaded
events which allow you to receive callbacks when a 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 has loaded and unloaded respectively. Unity raises the sceneLoaded
event after OnEnable
but before Start
for all objects in the scene. For details and example usage, refer to the relevant API reference pages.
For a diagram that includes scene load as part of the execution flow, refer to Details of disabling Domain and Scene reload
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
API reference for execution order information for methods marked with these types.
The following Animation loop callbacks shown in the diagram are called on 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 that derive from MonoBehaviour:
Additional animation-related event functions are called on scripts that derive from StateMachineBehaviour
:
StateMachineBehaviour.OnStateMachineEnter
StateMachineBehaviour.OnStateMachineExit
StateMachineBehaviour.OnStateEnter
StateMachineBehaviour.OnStateUpdate
StateMachineBehaviour.OnStateExit
StateMachineBehaviour.OnStateMove
StateMachineBehaviour.OnStateIK
Other animation functions shown in the diagram 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.
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.
OnPreCull
: Called before the 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 infoOnPreCull
is called just before culling takes place.OnBecameVisible
/OnBecameInvisible
: Called when an object becomes visible/invisible to any camera. OnBecameInvisible
is not shown in the flow chart above since an object may become invisible at any time.OnWillRenderObject
: Called once for each camera if the object is visible.OnPreRender
: Called before the camera starts rendering the scene.OnRenderObject
: Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.OnPostRender
: Called after a camera finishes rendering the scene.OnRenderImage
: Called after scene rendering is complete to allow post-processingA process that improves product visuals by applying filters and effects before the image appears on screen. You can use post-processing effects to simulate physical camera and film properties, for example Bloom and Depth of Field. More info post processing, postprocessing, postprocessOnGUI
: Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.OnDrawGizmos
Used for drawing GizmosA graphic overlay associated with a GameObject in a Scene, and displayed in the Scene View. Built-in scene tools such as the move tool are Gizmos, and you can create custom Gizmos using textures or scripting. Some Gizmos are only drawn when the GameObject is selected, while other Gizmos are drawn by the Editor regardless of which GameObjects are selected. More info
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.
Suspended coroutines can resume at different points in the execution sequence depending on the yield instruction used. For example, coroutines that use WaitForEndOfFrame
resume at the end of the frame, while those that use WaitForFixedUpdate
resume at the end of the fixed update step. For more information, refer to Coroutines.
Regular .NET Tasks and asynchronous methods resume in the Update
phase. Similarly to coroutines, Unity’s custom Awaitable
class can resume at different points depending on the method you use when awaiting. For more information, refer to Asynchronous programming.
Note: The exact order of execution between resuming coroutines and asynchronous tasks is not guaranteed. Awaitables are grouped together and executed in the order they were awaited.
When using the Entity Component System (ECS), Unity merges ECS system group updates into the Player update loop.
You can use the Entities Systems window to view the update order of ECS system groups relative to the full Player loop. For more information, refer to Update order of systems in the Entities package documentation.
In general, you can’t 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.
You can’t specify the order in which an event function is called for different instances of the same MonoBehaviour script. 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.
To configure the execution order between different MonoBehaviour scripts, refer to Script execution order.