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 7: Collections & Loops

Collections are groups of items. Unity’s Visual Scripting provides us two collection types List and Dictionary.


A list is a collection of objects sharing the same data type. The items stored in the list are sequentially ordered, meaning that the order in which items are added is maintained.

Unity Visual Scripting – Creating a List
Creating a List.

Creating a List

In the Blackboard inspector, create a new variable; at the type field, write down ‘list’ and select which data type the list should store. We can now add as many Values as we like to be part of the list.
We can also create a list by code using the Create List node.

Unity Visual Scripting – List Add Item, List Insert Item and Set Item nodes
List Add Item, List Insert Item and Set Item nodes.

Adding Items

A few nodes are available for adding items to the list.
With the List Add Item node, we connect the list and the item we want to add. The item will be appended to the end of the list.
If we want to add an item to a specific position in the list, we use the List Insert Item node. This node has a third input – Index, which is the intended position. Note that Index starts counting from 0.
Therefore the first item’s index will be 0, the second is 1, and so on... We can also replace an item at a given index position with another using the Set Item node.

Unity Visual Scripting – Remove At node
Remove At node.

Removing Items

With the Remove At node, we can delete an item from the list using its position in the index. We use the Remove node to delete a provided item with its value instead of its index.

Unity Visual Scripting – Get Item, First Item and Last Item nodes
Get Item, First Item and Last Item nodes.

Getting Items

The Get Item node, as its name suggests, retrieves the item stored at a given index.
We can fetch the first and last items using the First Item and Last Item nodes.

Unity Visual Scripting – Contains Item, Count Items and Clear nodes
Contains Item, Count Items and Clear nodes.

Item Exists?

We can ask whether or not an item exists in a list using the Contains Item node, providing it with a value.


When we need to know how many items a list contains, we use the Count Items node.

List Clear

Finally, the Clear node will remove all items from a list at once.


Another collection is the Dictionary, which allows us to map one value to another.
When we create a dictionary, we need to specify which data type for an item’s Key and another data type for its Value.
When adding an item to the dictionary, we specify what key will map to it.

Unity Visual Scripting – Create Dictionary
Create Dictionary.

For example, we may create a dictionary with a String for both the Key and the Value. Then, each item will have a person’s name as the key and the person’s home address for the value. We can then ask the dictionary whats the address for a given name is.

To managing dictionaries, we use nodes similar to those introduced for managing lists.


Loops allow us to repeat a section of our script multiple times.
Note that all iterations happen right away and will only stop when the condition is reached.

Unity Visual Scripting – While Loop node
While Loop node.

While Loop

The While Loop node has a boolean Condition input and two Flow outputs: Body and Exit. The node will execute the Body flow repeatedly until the condition turns from true to false. At this point, the node will run the Exit flow.
When can we use a While Loop? We may want an enemy to continue to chase the player as long as it’s seeing it. However, once the player is out of view, the chasing loop is halted.

Unity Visual Scripting – For Loop node
For Loop node.

For Loop

With the For Loop node, we can define how many repetitions the loop should go through. This node uses an Index to count the loop’s current iteration.
The First number is the index’s initial value, and the Last number is the index’s upper limit; we will only get up to the last number and quite the loop just before we reach it. The Step will define the amount to increment the index after each iteration.
An example of using a For Loop is spawning a specific number of enemies next to our player , configuring each one with a different weapon based on the Index value.

Unity Visual Scripting – For Each Loop and Break Loop nodes
For Each Loop and Break Loop nodes.

For Each Loop

The For Each Loop node iterates over a collection’s items. We pass in a list or a dictionary. The Item output provides us with the loop’s current iteration item.
We may use this node to go through a list of the game’s characters and move each one of them. We can also connect Dictionaries to this node by toggling on the Dictionary option.

Break Loop

With the Break Loop node, we can exit a loop at any given time. Furthermore, we can use it for any loop by using it inside the Body flow.

In the next part of this series, we will learn how to work and use GameObjects – our bridgfe for interfacing with Unity.

© All rights reserved.