Initialization and Regular Update Events
Learn about the usage of initialization and regular update event functions that Unity offers.
To implement most of the gameplay mechanics, we need to be familiar with the event-driven nature of Unity scripting. The scripts created inside the Unity Editor by default inherit the MonoBehaviour
class. This class provides access to event functions that can be defined by the developer, through which the gameplay can be implemented.
Event functions
A script created by the developer is not part of the Unity engine itself; instead, the Unity engine passes control to such scripts when some in-game event is triggered. These events are classified into four different categories as follows:
Initialization events: These events are emitted when an object is created or enabled; for example, the
Start
andAwake
methods, etc.Regular update events: These events are emitted regularly after some condition is met; for example, the
Update
,FixedUpdate
, andLateUpdate
methods, etc.Physics events: All events generated by Unity’s physics engine come under this umbrella. Examples include the
OnCollisionXXX
andOnTriggerXXX
methods, etc.GUI events: These events are emitted to handle UI-specific cases, such as the
OnGUI
method.
Moving forward, we’ll look into the detailed use cases of such event functions.
Initialization events
Initialization is done right after objects’ instantiation, which might occur at the start of the game lifecycle or somewhere in between. Therefore, initialization events in Unity cover all such use cases.
The Awake
event
This is the first method invoked for any script derived from the MonoBehaviour
class. This event is triggered for all active GameObjects present inside the scene when the scene is loaded.
The RedCube.cs
and BlueCube.cs
scripts below alter the size of the GameObject they’re attached with:
Get hands-on with 1400+ tech skills courses.