Introduction to Visual Scripting in Unity

In this chapter, we will start using Visual Scripting, a built-in feature available in Unity that lets you create game logic using visual nodes instead of traditional code. We’ll use Visual Scripting to build gameplay logic where the player collects items across multiple levels — from a maze to an island, and finally to a village.

After completing this chapter, you should be able to:

 Install and set up Visual Scripting in Unity.
 Understand and use basic Visual Scripting concepts such as variables, events, and nodes.
 Create and connect nodes to build simple game logic.
 Track and display the player’s score using variables.
 Detect collisions with specific tagged objects using visual scripts.
 Store the score using data that persists between scenes.
 Load new scenes based on score or progress.
 Implement logic to trigger a Win scene when all conditions are met.

You will build the logic gradually and visually — starting with something simple, like incrementing a value, and progressing to a complete system where the player picks up boxes, advances through levels, and sees a Win message at the end.

What Is Visual Scripting?

Imagine you’re assembling IKEA furniture. The manual shows you pictures of each part and how they fit together — you don’t need to read paragraphs of instructions. Visual Scripting in Unity works the same way. Instead of typing code, you connect visual nodes that represent actions, conditions, and logic.

With Visual Scripting, you can:

 Make things happen when the player presses a button.
 Detect when two objects collide.
 Keep track of scores and variables.
 Switch scenes or trigger win conditions.

All without writing traditional C# code!

Why Use Visual Scripting?

Let’s say you’re building a maze game and want to allow the player to collect items. With code, you’d need to know how to write OnTriggerEnter, how to compare tags, and how to use variables. But with Visual Scripting, you drag out blocks like puzzle pieces: “When this happens → do that.”

Visual Scripting is:

 Visual and intuitive – great for visual learners.
 Fast to test – prototype game logic quickly.
 Great for understanding logic – you’ll develop your game thinking without being stuck on syntax.

Whether you’re a beginner or a coder looking to speed up development, it’s a great tool to add to your kit.

Getting Started: Installing Visual Scripting in Unity

By default, Visual scripting should already be installed, and you can check that it is by looking at the Project window, and checking if you can see a folder called Visual Scripting in the folder Packages, as per the next figure:

However, if the folder is not present, you will need to install Visual Script; in that case, follow the next steps.

 Open the Package Manager: from the top menu, select Window | Package Manager.
 Switch to Unity Registry (i.e., click on the corresponding logo to the left; see next figure).
 Search for Visual Scripting: type visual scripting in the search field.
 Select the result Visual Scripting.
 Install that package.

Overview of the Visual Scripting Graph Window

Once the Visual Scripting package has been installed, it’s time to open the graph editor — your visual playground for building logic.

 Create a new scene (i.e., Basic Built-in) and save it as testScript.
 Create an Empty GameObject and rename it to gameLogic.
 With gameLogic selected, go to the Inspector.
 Click Add Component, then type Script Machine and add it.
 Under the Script Machine component, select Source | Embed (i.e., we’ll keep the script inside the object).
 Add a title to this script, for example “MyFirstScript”.
 Open the Graph Window by clicking on the button labelled “Edit Graph”.
 If a warning message appears, select the options to “Change Now”.
  •  The Visual Scripting Graph Editor will then open in a new window or tab, as per the next figure.

  • The Graph window is where all the action happens.
 Left Panel (Variables): Here you define and view your variables (like score, player health, etc.).
  • Center (Canvas): This is where you drag and connect nodes — like building blocks.
Think of it like building a LEGO model:
 Each node is a LEGO piece.
 Connections are the way the pieces snap together.
 The final creation is your working game mechanic.

How Visual Scripting Executes Logic

At the core of Visual Scripting are Events and Flows.

For example:

 Event: On Start — runs once when the game begins.
 Flow: what should happen after the event — e.g., set a variable, play a sound, load a level.

.

Each node has ports:

 Input ports (usually on the left or top).
 Output ports (on the right or bottom).

You click and drag from one port to another to connect nodes, just like connecting dots.

For example, the next figure shows a simple graph that logs “Hello World!” to the console when the game starts.

Tips for a Smooth Start

To work efficiently with the nodes of your visual scripts:

 Use the right-click menu to quickly add nodes.
 Use F to focus on selected nodes in the canvas.
 Use Ctrl/Cmd + D to duplicate nodes.
 Keep things tidy — a clean graph is easier to debug and understand.

What’s Next?

Now that you’ve installed Visual Scripting and opened your first graph, you’re ready to build logic without code.

In the next part of this chapter, we will:

 Create a counter that goes up when you press a key.
 Use that to introduce variables and events.

Soon after, we’ll build the logic to collect boxes, update the score, and move through the levels all the way to the Win scene.

Before we jump into collecting objects or loading levels, let’s take a moment to understand the core ideas behind Visual Scripting. These are the building blocks — like the grammar of a new language or the tools in your backpack before a hike.

We’ll explore:

 Variables (your game’s memory)
 Events (how things start)
 Nodes and flows (the logic highways)
 PlayerPrefs (how data survives across scenes)

Understanding these concepts will make your visual scripting smoother and way more fun.

Variables: Your Game’s Memory Bank

Think of a variable like a labelled jar on a shelf. You can store something in it (like a score), check what’s inside, or change it whenever you need. In Visual Scripting, you use variables to:

 Keep track of score.
 Store if an item was picked up.
 Count how many collectibles the player has found.

Types of Variables

In Unity, Visual Scripting offers a few kinds of variables such as float, integer, string, Boolean or even vectors.

Some of these variables can be local to a single graph (graph variables), stored on a specific GameObject (GameObject variables), available across all objects in the scene (scene variable), global across all scenes (application variable) or even stored after quitting (saved variable).

Variables are usually available from the Script Graph Panel (to the left of the screen).

Creating a Counter Variable

Let’s create a simple counter variable:

 Open your Script Graph (if it is not already open).
 In the Variables panel (left side), click the Graph tab.
 Type a name for the new variable, for example counter.

 Click the + icon (to the right of the name of your variable) and a new attribute called Type (Null) will appear under its name.

A screenshot of a computer

AI-generated content may be incorrect.

 Click on the arrow to the right of the attribute Type, set the type to Integer and give it a starting value (e.g., 0)

Congratulations! You now have a just created a variable your game can update, display, and check later.

Events: The Triggers That Start Everything

Think of events like someone ringing your doorbell. You didn’t know when it would happen, but once it does, it triggers a response — you go to the door.

In your game, events include:

 Game starts: the event is On Start.
 Object enters a trigger: the event is On Trigger Enter.
 Player presses a key: the event is On Key Down.
 The screen is refreshed every frame: the event is Update.

These event nodes are usually the starting point of your logic flows.

Adding an Event Node

Let’s create a new event:

 Right-click on your Graph canvas.
 In the contextual search field, type keydown and select the option GetKeyDownKey.

.

 This will create a new node labelled “Get Key Down” that will be fired every time a key (to specify) is pressed.

You can now connect nodes from this event to make things happen.

Nodes and Flows: The Language of Logic

Nodes are the heart of Visual Scripting — each one is a tiny machine that does one thing:

 Add numbers.
 Compare values.
 Destroy objects.
 Load scenes.

.

.

Every node has ports:

 Control Ports (Flow): Like power lines that say “Go now”
 Data Ports (Values): These carry variables, objects, numbers, etc.

.

Imagine a train track system:

 Trains (logic) follow tracks (flow)
 Cargo (data) is moved between stations (nodes)
 Junctions (branches) control direction based on conditions

Linking Nodes Together

Let’s link a few nodes now to see how this works:

 From the On Start node, drag the Control Output (small arrow) into empty space.
 Release the drag, type debug, and choose Debug → Log

 This will create a new node called “Debug Log”.

 From that node, drag the mouse from the input, as per the next figure, type “string literal” and choose “String literal”.

 This will create new node called String; type “Hello World” in the corresponding field.

You can now play the scene, and you should see the message “Hello World” in the Console window, as per the next figure.

A screenshot of a computer

AI-generated content may be incorrect.

Conditions and Branches: Making Decisions

Sometimes your game needs to choose — “Has the player collected 4 boxes?” or “Did they press a certain key?”

In that case, you can use the If node to make yes/no decisions.

It’s like a fork in the road: if something is true, go left; if false, go right.

Try a Simple Condition

 Create an If node: Right click on the canvas, type “if” in the search field, and select the option “If (in Control)”

 This will create a new node called If, as per the next figure.

 Connect its input from the output of the event On Start node.

 Drag the input of the If node and select Boolean Literal.

 This will create a new node called Boolean.
 Check the box in that node, to set the value of this variable to True (i.e., box ticked).

 Add a Debug Log node to each output that displaysYou Win!to the output True and Keep playing to the output False, as per the next figure (you can use String Literals for each message).

 You can now play the scene, and you should see the message “You Win” in the console window.

Saving Data Across Scenes with Saved Variables

So, your player collects boxes in the Maze, and then… the level changes — but what happens to the score?

This is where Saved Variables come in. It’s Unity’s way of storing data like a sticky note between scenes or game sessions.

You’ll use it to:

 Save the score before loading a new level.
 Load it again at the start of the next level.
 Check if the player collected enough items to win.

Storing and Retrieving Between Scenes

Let’s create a saved variable:

 Click on the Saved Variable tab in the Graph Inspector and create a new integer variable called score.

 Right-click on the canvas, type set saved and select Set Saved Variable.
 Select the result Set Saved Variable.

A screenshot of a computer program

AI-generated content may be incorrect.

 This will create a new node called Set Variable.
 Type score for the variable’s name, as per the next figure.

 Drag the other input from that node.

 Select Integer Literal from the search results.

  It will create a new Integer node; set that integer to 3, as per the next figure.

So, in the previous steps, and while these two new nodes are not yet connected to an event, we set the variable score to 3, and this variable can be read later, regardless of the scene that is currently loaded; this is because Saved Variables persist across scenes during the session.

Creating Your First Script

Now that you’ve installed Visual Scripting and learned the basics of variables, events, and nodes, let’s get hands-on and build your first working script. We’ll start small — by creating a counter that goes up every time the player presses a key. This is like building your first LEGO model: simple, satisfying, and the beginning of bigger things. This script will help you understand how variables, input events, and flow work together. Once you’re comfortable with this, you’ll be able to expand into more complex logic like item collection and scoring.

What Are We Building?

Let’s say you want the game to count how many times the player presses the spacebar. Each press increases a number shown on the screen. This could represent:

 A jump counter.
 A coin counter.
 Or just a simple test of your logic system.

.

We’ll use:

 A Graph Variable to store the count
 The Get Key Down node to detect spacebar presses
 The Add node to increase the count.

Create a Graph to Handle Logic

Now let’s create the logic that increases the value of the counter.

First, we will create an Empty GameObject for the logic:

 Deactivae the object visualScripting that we have created earlier, as we won’t be using it for now.
 Create an empty object and rename it to gameLogic.
 Select that object, and in the Inspector, click Add Component | Script Machine.
 Set the attribute Source to Embed
 Set a title for that graph, for example counterGraph.

 Click Edit Graph to open the Visual Scripting editor

.

Next, we will Create a Variable for the Counter

 In the Variables panel to the left, click the Graph tab.
 Set the name of the variable to counter and click the + icon.

 Set its type to Integer and the initial value to 0.

Detecting the Keyboard

Next, we will detect when the player presses the space bar:

 Right-click in the canvas.
 Type get key down in the contextual search field.

 Select the result Input: Get Key Down (Key).
 This will create a new node called GeyKeyDown.

A screenshot of a computer program

AI-generated content may be incorrect.

 In the Key dropdown, select the item Space

This node is now set-up, and it will be triggered each time the spacebar is pressed.

Increasing the counter

In this section, we’ll increase the counter by 1 every time the space bar is pressed. For this, we will create and combine several nodes to create an integer (1), get the value of the variable counter, and set the counter variable with its previous value + 1.

 Right-click on the canvas, type counter, and select the result Get Counter.

 This will create a node called Get Variable.

 Right-click on the canvas, type counter, and select the result Set Counter. This will create a new node called Set Variable.

  Right-click on the canvas, type add, and select the result Add (in Math/Generic)

 This will create a new node called Set Add.

.

Once this is done, it is time to combine these nodes.

 Connect the output of the node GetKey Down to the node Set Variable.
 Connect the output of the node Get Variable to the A input of the node Add Inputs.
 Connect the output of the node Add Inputs to the input of the node Set Variable.

.

Finally, we need to add one to the variable counter so drag the B input from the node Add Inputs, select Integer Literal, and set it to 1.

So, based on the entire graph, whenever the player presses the space key, the variable counter will be increased by 1.

However, so that the keys can be detected we need to make sure that the counter is increased only when we have pressed the Space key:

Connect the OnUpdate node to the GetKeyDown node, this ensures that the game listens to the keyboard.

Next, we need to modify the connection between the nodes Get Key Down and Set Variable; at present, the variable is set, regardless of whether the space key has been pressed; instead, we should check that it is true that the space key has been pressed before setting the variable counter.

 So please modify this connection as per the next figure:

In the previous figure, we use an If node between the nodes Get Key Down and Set Variable. This conditional statement is based on the value of the output of the node Get Key Down; if it is true, then we proceed (flow) to the node Set Variable.

Finally, to be able to print the value of the score, we could just add a Debug Log node to display the value of the counter in the console window:

 Add and connect a Debug Log node to the Set Variable node as per the next figure.

In the previous graph, we just added a Debug Log node, for which the message is the value of the variable counter.

You can now play the scene, and press space bar (after selecting the Game window, so that the space bar is detected), and you should see corresponding messages in the Console window.

Summary

In this chapter, we have learned how to use Get Key Down to respond to player input, allowing us to trigger actions when specific keys are pressed. We also explored how to create and update variables, which store and track information during gameplay. By using basic math nodes such as Add, we were able to manipulate numerical values—an essential skill for implementing game mechanics. Additionally, we discovered how to modify UI elements in real-time, giving immediate feedback to the player based on their actions.

This simple logic forms the foundation for more advanced game features such as collecting items, implementing scoring systems, managing health bars, and tracking inventory. Mastering these basics enables you to build more interactive and responsive game experiences

A few tips

As you create your first visual scripts, you will probably need to undo or remove elements or connections, the following should be helpful:

 Undo an action in the graph: CTRL + Z.
 Redo an action in the graph: CTRL + SHIFT + Z.
 Remove a connection: Right-click on it.
 Remove a node: Right-click on it and select Delete.
 Zoom-in/out: use the mouse wheel.
 Pan the graph: click the middle mouse button and drag your mouse.
 Add a new node: Right-click on the graph and type the node name, or right-click, select “Add Node” and then type the node name.

Level roundup

Let’s quickly review the key concepts we’ve learned:

 Variables store game data like score or state.
 Events start logic flows based on triggers like key presses or collisions.
 Nodes and Flows connect the logic in a readable, visual way.
 Conditions (Branches) let you make decisions.
 Saved Variables store values between scenes (and even after restarting the game).

.

Quiz

It is now time to test your knowledge. Please state whether the following questions are TRUE or FALSE; the solutions are on the next page.

1.Visual Scripting in Unity allows developers to create game logic without writing any traditional code.
2.Variables in Unity Visual Scripting can only be used within a single graph and cannot persist between scenes.
3.The Visual Scripting Graph Editor can be opened by clicking “Edit Graph” in the Script Machine component.
4.The On Trigger Enter event node is used to detect when two objects start overlapping.
5.You can only use PlayerPrefs to save a score between scenes in Unity Visual Scripting.
6.The “Branch” node is used to destroy GameObjects in Unity Visual Scripting.
7.The “Debug Log” node allows you to print messages to the Unity Console window.
8.Unity Visual Scripting supports creating conditions using Boolean variables and the If node.
9.Saved variables in Visual Scripting are suitable for keeping score data between scenes during the same game session.
10.To create a counter in Visual Scripting, you must define a variable and increment it using appropriate nodes.

.