Event function execution order
Understand the execution order of Unity's built-in event functions so you can respond to events and update the state of your game in the right order.
Read time 6 minutesLast updated 10 days ago
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.
Order of execution for event functions during the lifecycle of a MonoBehaviour script.
Internal physics update
The nodes labeled Internal physics update in the previous diagram represent the physics simulation. The physics simulation can occur at different stages of the MonoBehaviour execution sequence depending on the physics configuration in your project. The options are as follows:
- If is set to
Physics.simulationModeorSimulationMode.FixedUpdateis set toPhysics2D.simulationMode, the physics simulation step occurs directly afterSimulationMode2D.FixedUpdate.MonoBehaviour.FixedUpdate - If is set to
Physics.simulationModeorSimulationMode.Updateis set toPhysics2D.simulationMode, the physics simulation step occurs directly afterSimulationMode2D.Update.MonoBehaviour.Update - If is set to
Physics.simulationModeorSimulationMode.Scriptis set toPhysics2D.simulationMode, then you manually simulate physics from your script code, and the timing of the simulation depends on the step parameter you pass toSimulationMode2D.ScriptorPhysics.Simulate.Physics2D.Simulate
The physics simulation step includes:
-
All the stages of collision detection.
-
Rigidbody and joint integration.
-
Writing back body poses to Transforms.
-
Invoking the relevant physics MonoBehaviour callbacks, which include:
- ,
MonoBehaviour.OnTriggerEnter,MonoBehaviour.OnTriggerStay,MonoBehaviour.OnTriggerExit,MonoBehaviour.OnTriggerEnter2D,MonoBehaviour.OnTriggerStay2DMonoBehaviour.OnTriggerExit2D - ,
MonoBehaviour.OnCollisionEnter,MonoBehaviour.OnCollisionStay,MonoBehaviour.OnCollisionExit,MonoBehaviour.OnCollisionEnter2D,MonoBehaviour.OnCollisionStay2DMonoBehaviour.OnCollisionExit2D - ,
MonoBehaviour.OnJointBreakMonoBehaviour.OnJointBreak2D - ,
MonoBehaviour.OnParticleCollisionMonoBehaviour.OnParticleTrigger
Internal animation update
The following diagram shows the order of execution for the regular Animation update loop, and expands the nodes labelled Internal animation update in the previous diagram:
Order of execution for the regular Animation update loop.
The following Animation loop callbacks shown in the diagram are called on scripts that derive from MonoBehaviour:
Additional animation-related event functions are called on scripts that derive from :
StateMachineBehaviourStateMachineBehaviour.OnStateMachineEnterStateMachineBehaviour.OnStateMachineExitStateMachineBehaviour.OnStateEnterStateMachineBehaviour.OnStateUpdateStateMachineBehaviour.OnStateExitStateMachineBehaviour.OnStateMoveStateMachineBehaviour.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 Profiler 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 markers.
Rendering
This execution order applies for the Built-in Render Pipeline only. For details of execution order in render pipelines 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.
- : Called before the camera culls the scene. Culling determines which objects are visible to the camera.
OnPreCullis called just before culling takes place.OnPreCull - /
OnBecameVisible: Called when an object becomes visible/invisible to any camera.OnBecameInvisibleis not shown in the flow chart above since an object may become invisible at any time.OnBecameInvisible - : Called once for each camera if the object is visible.
OnWillRenderObject - : Called before the camera starts rendering the scene.
OnPreRender - : Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.
OnRenderObject - : Called after a camera finishes rendering the scene.
OnPostRender - : Called after scene rendering is complete to allow post-processing of the image, see Post-processing Effects.
OnRenderImage - : 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.
OnGUI - Used for drawing Gizmos in the scene view for visualisation purposes.
OnDrawGizmos
Resumption of coroutines and asynchronous tasks
Suspended coroutines can resume at different points in the execution sequence depending on the yield instruction used. For example, coroutines that use resume at the end of the frame, while those that use resume at the end of the fixed update step. For more information, refer to Coroutines.
WaitForEndOfFrameWaitForFixedUpdateRegular .NET Tasks and asynchronous methods resume in the phase. Similarly to coroutines, Unity's custom class can resume at different points depending on the method you use when awaiting. For more information, refer to Asynchronous programming with the Awaitable class.
UpdateAwaitableCombining MonoBehaviours with Entities
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.
Executing code outside the MonoBehaviour lifecycle
The event functions shown in the diagram are all part of the MonoBehaviour script lifecycle, but there are many other events and callbacks that Unity invokes outside of this lifecycle. For example, you can execute custom setup or cleanup code before your scripts are loaded or after they are unloaded with the code lifecycle callbacks in the namespace. You can also execute code on entry to and exit from Play mode with the lifecycle attributes in the and namespaces. For more information, refer to Code reload and the code lifecycle.
Unity.Scripting.LifecycleManagementUnityEngineUnityEditor.Scripting.LifecycleManagementLimitations
In general, you can't rely on the order in which the same event function is invoked for different GameObjects, 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 function of one MonoBehaviour might be called before or after the function for the same MonoBehaviour on another GameObject, including its own parent or child GameObjects.
UpdateUpdateTo configure the execution order between different MonoBehaviour scripts, refer to Script execution order.