Event functions are a set of predefined callbacks that all MonoBehaviour script components can potentially receive. The callbacks are triggered by various Unity Editor and Engine events, including:
Implement the appropriate method signature in your MonoBehaviour
-derived class to allow your game objects to react to the source events.
For a full list of the available callbacks, refer to the MonoBehaviour
API reference where they are listed under Messages. The rest of this section gives an overview of some of the key groups of event functions.
A playing game continuously renders a series of frames. A key concept in games programming is making changes to position, state, and behavior of objects just before each frame is rendered. The Update
function is the main place for this kind of code in Unity. Update
is called before the frame is rendered and also before animations are calculated.
The physics system also updates in discrete time steps in a similar way to the frame rendering. A separate event function called FixedUpdate
is called just before each physics update. Since the physics updates and frame updates don’t occur with the same frequency, you can get more accurate results from physics code if you place it in the FixedUpdate
function rather than Update
.
It’s also sometimes useful to make additional changes at a point after the Update
and FixedUpdate
functions have been called for all objects in the scene, and after all animations have been calculated. Some examples of this scenario are:
The LateUpdate
function can be used for these kinds of situations.
For an overview of how frame and physics updates fit into the overall event function update sequence, refer to Event function execution order.
It’s often useful to be able to call initialization code in advance of any updates that occur during gameplay. The Start
function is called before the first frame or physics update on an object. The Awake
function is called for each object in the scene at the time when the scene loads. Note that although the various objects’ Start
and Awake
functions are called in arbitrary order, all instances of Awake
will have finished before the first Start
is called. This means that code in a Start
function can make use of other initializations previously carried out in the Awake
phase.
For an overview of how initialization functions fit into the overall event function update sequence, refer to Event function execution order.
For projects that use the legacy IMGUI UI system, the MonoBehavior.OnGUI
callback is called periodically to draw and manage UI elements.
Note: Adding an OnGUI
callback, even if empty, to any object in a scene also adds IMGUI processing to the frame handling process, which creates extra overhead. Only use OnGUI
if your project uses the IMGUI UI system, which is no longer recommended.
For more information, refer to UI systems.
The MonoBehaviour
class also has a set of event functions named with the prefix OnMouse
(e.g., OnMouseOver
, OnMouseDown
) for reacting to user actions with the mouse. These callbacks are only supported for projects using the legacy Input ManagerSettings where you can define all the different input axes, buttons and controls for your project. More info
See in Glossary, which is no longer recommended.
For more information, refer to Input.
The physics system reports collisionsA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary against an object by calling event functions on that object’s script component.
The OnCollisionEnter
, OnCollisionStay
and OnCollisionExit
functions are called as contact is made, held and broken. The corresponding OnTriggerEnter
, OnTriggerStay
and OnTriggerExit
functions are called when the object’s colliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary is configured as a trigger collider.
For more information on physics event functions, refer to Collider interactions.