2019 2022

Thank you for taking part in the NotSlot journey!
We invite you to join us in our next chapter:

  • by nir,
  • Danielle Elias

Unity Visual Scripting Part 3: Lifecycle Events

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.

Unity Visual Scripting – GameObject Lifecycle
GameObject lifecycle events in a Visual Scripting graph.

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.

Unity Visual Scripting – Debug Log node
Debug Log node.

On Start

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.

Unity Visual Scripting – On Start node
On Start node.

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.

On Update

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.

Unity Visual Scripting – On Update node
On Update node.

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.

Unity Visual Scripting – On Fixed Update node
On Fixed Udpdate node.

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.

Unity Visual Scripting – On Late Update node
On Late Update node.

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.

Unity Visual Scripting – On Enable node
On Enable node.

On Enable

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.

Unity Visual Scripting – On Disable & On Destroy nodes
On Disable & On Destroy nodes.

On Disable

On Disable will be triggered when the object is de-activated.

On Destroy

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.

© All rights reserved.