In this article, we will get familiar with the Lifecycle Events, nodes triggered by Unity that are usually the starting points of our visual script graphs.
Like any game engine, Unity runs our games by executing a giant loop, repeating over and over to create and update each frame.
With Lifecycle Events, we can tap into the game loop stages and apply our custom behaviors. We can also react to various states of a game object.
There are many events, such as Input Events and Physics Events; we’ll come to know them as this series progresses. But, for now, we will focus on the Lifecycle Events; those are the events executed during the object’s appearance in the game.
As you may notice, the Event Nodes are distinguished with a green strip.
To help explain the differences between the Lifecycle events, I will use the Debug Log node. With this node, we can print messages to Unity’s Console window. For example, this Log will print “Hello” in the console.
The On Start event is called the first time an object appears in the game. This node is called only once for a given script graph.
For example, above is the Debug Log node connected to the On Start event. Once we enter play mode, the game will print “Hello” to the console one time.
When it comes to rendering, a game is like an animation where each animation frame is generated on the go.
The On Update event runs for each frame that occurs while playing; It’s the beating heart of our game, allowing us to adapt to the player in real-time.
In this example, we connect the Debug Log node to the On Update event. Once we enter play mode, the game will print “Hello” to the console on every frame as long as we play.
On Fixed Update
On Fixed Update node is triggered 50 times a second in a constant interval. We use it instead of On Update when interacting with physics. We will discuss Rigidbody and physics in a later video.
On Late Update
The On Late Update node is similar to the Update event but is triggered after all Update and Fixed Update events in all scripts.
We can use On Late Update when we want a camera to follow a moving object powered by an Update event.
On Enable will be triggered every time the object is activated.
For example, this may be used when a level is loaded, a GameObject is dynamically instantiated or re-activating a previously hidden object.
On Disable will be triggered when the object is de-activated.
On Destroy will be triggered immediately before an object is deleted or removed entirely from the level.
In the upcoming article, we will look into asking questions with Logic nodes, the first step towards more complex scripts.
Hey! Before you move on...