Key Event Functions You Should Know in Unity! Types and Usage

When creating games or apps in Unity, you write scripts (programs) to move characters or display items. Event functions help determine what actions to perform and when to perform them.

For example, you might want certain actions to execute when the game starts, or when a character collides with an enemy. Event functions tell you when these actions should occur.

Mastering event functions allows you to control your game exactly as intended. This blog post clearly explains the different types of event functions and how to use each one, making it easy to understand even for Unity beginners.

PR

What is an event function?

Event functions are special functions that Unity automatically calls at specific times. For example, there are functions called at various times such as “when the game starts,” “when a character collides with something,” or “when processing is repeated every frame (dozens of times per second).”

The Role of Event Functions

Event functions execute script processing in response to various events within the game. This allows you to control timing to ensure the game operates correctly. For example, displaying a character when the player starts the game or processing damage dealt to enemies can be achieved using event functions.

Basic Concepts of Event Functions in Unity

When writing scripts in Unity, scripts inherit from a class called MonoBehaviour. This MonoBehaviour class provides Unity’s fundamental functionality, and event functions are also inherited from this class.

For example, the Start() event function is called only once when a game object becomes active. The Update() event function is called every frame and is used to perform repetitive processing within the game.

By defining these event functions within scripts that inherit from MonoBehaviour, Unity automatically calls them at the appropriate times. This mechanism is essential for efficiently running games and applications.

PR

List of Major Event Functions

Below is a tabular list of Unity event functions.

CategoryEvent FunctionDescriptionUse Case
Initialization SystemAwake()Called first when the GameObject is initializedSet up object defaults, establish dependencies
Start()Called on the first frame when the GameObject is enabledInitialization at the start of the game, settings
Update SystemUpdate()Called once per frameGame logic, handling user input
FixedUpdate()Called at a fixed frame ratePhysics calculations and simulation
LateUpdate()Called after Update()Camera follow behavior, post-processing of animations
Collision Detection SystemOnCollisionEnter(Collision collision)Called when colliding with another objectHandle collision effects, trigger reactions
OnTriggerEnter(Collider other)Called when entering a trigger colliderHandle entering a specific area
OnCollisionStay(Collision collision)Called every frame while collidingContinuous processing during collision
OnTriggerStay(Collider other)Called every frame while staying in a trigger areaContinuous processing inside the trigger
OnCollisionExit(Collision collision)Called when a collision endsHandle end-of-collision events
OnTriggerExit(Collider other)Called when exiting a trigger areaHandle leaving a trigger area
OtherOnEnable()Called when the object is enabledDisplay object, load resources
OnDisable()Called when the object is disabledHide object, release resources
OnDestroy()Called when the object is destroyedCleanup, prevent memory leaks
OnApplicationQuit()Called when the application quitsHandle application shutdown
PR

Initialization-related events

Awake()

Awake() is the event function called first when a game object is created. Since this function executes before any other scripts, it is ideal for initializing the object. For example, processes that reference and configure other scripts or components should be performed in Awake().

Points:

– Called regardless of whether the game object is active or not.

– Ideal for initializing data and setting dependencies.

Is Awake() called when the game object is inactive?

GameObjects that were inactive when the game started will not have their Awake() method called.

Note that it is called when they become active.

Start()

Start() is an event function called during the first frame after a game object is activated. Since this function executes after the object is fully prepared, it is suitable for performing initialization tasks at the start of the game. For example, tasks like setting the player’s position at the game start should be handled in Start().

Key points:

  • It is only called after the game object is activated.
  • It performs processing with other scripts and components already initialized.

How to Use Awake() and Start()

Both Awake() and Start() are used for initialization, but they are called at different times, so they are used as follows:

  • Awake() is called first when an object is instantiated, so it’s used for tasks that don’t depend on other objects, such as setting variables or dependencies within the script. For example, place any processing you absolutely want to be done before this script runs inside Awake().
  • Start() is called only when the object is activated, so it performs tasks that assume all other objects and scripts are ready. For example, tasks like setting initial positions or statuses using other objects or data in the scene should be written in Start().

PR

Update events

In game development, frequently repeated processes such as character movement and input handling are essential. Unity provides functions called update events to perform these tasks. These are called every game frame to ensure smooth motion.

Update()

Update() is an event function called every frame. Since it runs each frame, it’s ideal for handling in-game movement and input processing. For example, you would write the movement logic for player controls using a keyboard or gamepad inside Update().

Primary uses:

– Player input processing (movement, jumping, attacking, etc.)

– Enemy and object movement

– Timer updates

FixedUpdate()

FixedUpdate() is an event function called at a fixed frame rate. Because it executes at regular intervals, it is used for processing related to physics calculations and physical behavior. While Update()’s frame rate fluctuates depending on PC performance, FixedUpdate() is called at consistent intervals, enabling accurate physics calculations.

Primary uses:

Physics calculations (gravity, collisions, applying forces, etc.)

Object movement using Rigidbody

LateUpdate()

LateUpdate() is an event function called after Update(). Since it executes after all Update() calls have completed, it’s used for tasks that should occur after other processing finishes. For example, when a camera follows a character, it helps adjust the camera position after the character’s movement is finalized.

Primary uses:

– Camera following logic

– Post-processing for animations or position adjustments

– Adjustments based on other objects’ movements


Update()、FixedUpdate()、LateUpdate()の使い分け

  • Update(): Executes every frame, so use it for processes requiring real-time responses, such as player input or enemy movement.
  • FixedUpdate(): Leave physics-related processing to FixedUpdate(), which is called at a fixed frame rate. This ensures stable movement and prevents unexpected behavior.
  • LateUpdate(): Use LateUpdate() for processes you want to execute after other tasks complete, especially for camera tracking and similar operations.
PR

Collision Detection System Event

In-game, characters may collide with enemies or walls, or enter specific areas. To detect these “collisions” or “contacts,” Unity provides collision detection events. Using these events allows you to control various reactions within the game.

OnCollisionEnter()

OnCollisionEnter() is an event function called when objects collide. It is automatically invoked upon collision and processes the collision information at that moment.

Primary uses:

Character reactions upon hitting walls or ground (stopping, bouncing, etc.)

Damage processing upon collision with enemies or obstacles

Triggering effects (explosions, shockwaves) during collisions

Important notes:

For OnCollisionEnter() to function, the object must have a Rigidbody component.

The object’s Collider must be configured to detect physical collisions.

OnTriggerEnter()

OnTriggerEnter() is an event function called when an object comes into contact with an object set as a trigger. Triggers detect “contact” rather than physical collisions. Using this function allows you to detect entry into specific areas or trigger events when certain conditions are met.

Primary uses:

  • Trigger events when characters enter specific areas (e.g., doors open, items appear)
  • Checkpoint detection
  • Activate special effects when entering hidden areas

Important Notes:

  • To use an object as a trigger, you must enable the “Is Trigger” option in its Collider component.
  • A Rigidbody is not required, but it is often advisable to attach one to dynamic objects.

The Difference Between OnCollisionEnter() and OnTriggerEnter() and When to Use Each

  • OnCollisionEnter() is used when a physical collision occurs. For example, it’s suitable for handling situations like the player colliding with a wall or ramming into an enemy.
  • OnTriggerEnter() is used to detect when an object “enters” a specific area, not for physical collisions. It’s useful for managing progression and events within the game.
PR

Other Events

In Unity, event functions are also called when objects are enabled, disabled, or destroyed. Using these event functions enables efficient state management and resource management.

OnEnable() / OnDisable()

OnEnable() and OnDisable() are event functions called when an object is enabled (activated) and when it is disabled (deactivated). By using these functions, you can control the processing that occurs when an object is displayed or hidden.

Primary uses:

  • Load resources when the object is activated (e.g., play music, display particles).
  • Release resources when the object is deactivated (e.g., stop animations or effects).
  • Execute specific processing based on the object’s state (e.g., toggle UI display).

OnDestroy()

OnDestroy() is an event function called when an object is destroyed. When an object is destroyed, the data in memory is also freed. Use OnDestroy() to perform post-processing on resources and data associated with this object. This prevents memory leaks (problems where unnecessary memory is not freed).

Primary uses:

  • Save data before the game object is destroyed (e.g., saving game progress).
  • Release references to other objects and systems to free up memory.
  • Clean up resources such as closing network connections and files.

OnApplicationQuit()

OnApplicationQuit() is an event function belonging to Unity’s MonoBehaviour class, called immediately before the application terminates. This function allows you to perform necessary cleanup tasks or save application state upon exit.

Key Features

  • Timing:
  • This method is called only once, immediately before the application terminates. This occurs regardless of the termination trigger—whether the user closes the game, stops playback in the editor, or the application crashes.
  • Purpose:
  • Save data: Save game progress and settings.
  • Disconnect connections: Properly terminate network connections and communications.
  • Free resources: Release memory currently in use.
  • Important Notes:
  • In iOS and Android apps, this method is not called when the app moves to the background. Therefore, tasks like saving data upon termination must be combined with other methods (such as OnApplicationPause()).