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 8: Game Objects

A Game Object is any object that appears in our game’s scene – whether it’s a character, tree, camera, or light, if it’s in the scene, it’s represented by a Game Object.
Every Game Object contains Components that define its properties and behavior. Adding components and tweaking their configuration differentiates between a character, a light, or a camera object. Since every Game Object is in the game’s scene, it has at least a Transform component controlling its location in the world.

Unity Visual Scripting – This and Game Object Literal nodes
This and Game Object Literal nodes.

Referencing

When we want to refer to a Game Object, we can use the This node, which will provide us the Game Object that the script is attached to. We can also use the Game Object Literal node to select a Game Object from the scene or a Prefab asset.

Unity Visual Scripting – Set Active, Get Active Self and Get Active In Hierarchy nodes
Set Active, Get Active Self and Get Active In Hierarchy nodes.

Activation

An inactive Game Object exists in the scene but isn’t visible, nor does it or its components update.
We may want to prepare a few enemies at our level and activate them progressively.
To activate and deactivate a Game Object, we can use the Set Active node, passing it an activity boolean. This is like manually unchecking the check box in the Inspector.

We have two nodes to ask whether a Game Object is active or not; Get Active Self will tell us if the Game Object itself is marked as active, as in whether its Inspector’s checkbox is ticked on or off. The Get Active In Hierarchy node will check whether the Game Object is active and whether all of its parents are active. Usually, we use the Hierarchy version as it’s more practical.

Unity Visual Scripting – Instantiate, Create Game Object and Add Component nodes
Instantiate, Create Game Object and Add Component nodes.

Creation

When making new Game Objects, we have two paths to choose from: duplicating an existing object or defining a new one.

To duplicate an existing Game Object, we can use the Instantiate node and pass it a scene Game Object or a Prefab asset. Perfect for creating enemies in real-time, shooting bullets, etc.

We use the Create Game Object node to create an empty Game Object while we play. We can also use the node variant with a Name input to control the game object’s name. The created Game Object is then available to us through the Result output; we can use it to set the position or add new components using the Add Component node.

Unity Visual Scripting – Get Component and Get Game Object nodes
Get Component and Get Game Object nodes.

Components

Speaking of components, we can access a Game Object’s Component with the Get Component passing the desired component type.
We can also go the other way around; if we have access to a component such as a Collider, we can use the Get Game Object node to retrieve the Game Object the Component is attached to.
Remove a Component from a Game Object is achieves using the Component: Destroy node.

Unity Visual Scripting – Destroy nodes
Destroy nodes.

Destroying

We can use the Destroy node to remove a Game Object from the scene in real-time, along with all its components and scripts. It’s helpful if we defeat an enemy and now want to remove it from the world.
In addition, we have a variant of Destroy with time which gives us the option to delay destroying the object.

Unity Visual Scripting – Get Tag and Set Tag nodes
Get Tag and Set Tag nodes.

Tags

We can assign tags to Game Objects to categorize them.
For example, we may use a “Collectable” tag to define all items in the scene the player can collect.
Tags aid us in identifying Game Objects to treat them differently in our scripts.
In the Game Object’s Inspector, we assign a tag in the dropdown near the name.
We can either choose from Unity’s predefined tags or create a new one by choosing Add Tag.

Use the Get Tag node to retrieve the Game Object’s tag.
We can assign a different tag to a Game Object with the Set Tag node.
With the Compare Tag node, we can confirm if a Game Object’s tag matches another; we can use the resulting boolean to run a script only on specified objects.

Unity Visual Scripting – Find Game Object With Tag and Find nodes
Find Game Object With Tag and Find nodes.

Finding

To look up Game Objects with a specified tag in the scene, we got the Find Game Object With Tag node. With this node, we can search for a Game Object at the scene by its tag; there are two variants for the node, one that returns the first matching object and another that returns all objects with the assigned tag.

We can also use the Find node to get a Game Object by its name.


Thank you for following along!
Unity’s Visual Scripting provides us with tons of handy nodes to use, and while we did not cover all of them, you now got everything you need to know to start creating your games. We will explore more nodes in the following articles, interacting with Unity’s different systems, such as Physics, Input, and more...

© All rights reserved.