Variables
Variables act as a container for a piece of information that might change as an application runs. To define a variable, you need to provide:
- A name for the variable, such as
MyVariable
. - The type of data the variable holds, such as
int
orstring
. - A value for the variable, such as
1
orcat
.
In Visual Scripting, you can give a node the name of a variable, instead of a fixed value or text. Your Script Graph uses the variable's name to access its value. For example, you can use a variable called Count
, with an int
type and a value of 1
. You can use an Add node in Visual Scripting to add 1 to the value of Count
, and save the new value in Count
to use again in another part of your Script Graph, or a different Script Graph.
Variables also have scopes. A variable's scope determines what parts of your Script Graph can access which variables to read or modify their values. The scope can also decide whether another Script Graph can access a variable.
You can create and manage variables in a graph from the Blackboard. For more information on the Blackboard, see The Blackboard. For more information on how to use variables, see Create and add a variable to a Script Graph.
Variable scopes
Each variable scope has its own tab on the Blackboard, except Flow variables. Visual Scripting has six variable scopes.
Variable Scope | Property |
---|---|
Flow Variables | Flow variables are like local variables in a scripting language: they have the smallest scope. You can't use a Flow variable if:
|
Graph Variables | Graph variables belong to a specific Script Graph. You can't access or modify Graph variables outside the specific Script Graph where they're defined. You also can't create a new Graph variable unless you have a Script Graph open in the Graph window. |
Object Variables | Object variables belong to a specific GameObject. You can edit an Object variable from the Unity Editor's Inspector for the GameObject, and the Object variable is accessible in all graphs attached to the GameObject. You can't create a new Object variable unless you've opened your Script Graph from a Script Machine component on a GameObject. |
Scene Variables | Scene variables belong to the current scene. Visual Scripting creates a new GameObject in your scene to hold references to your Scene variables. You can access your Scene variables from any Script Graph attached to a different GameObject in a single scene, but can't access a Scene variable in another scene in your project. |
App or Application Variables | Application variables belong to your entire application. You can access an Application variable across multiple scenes while your application runs, and the Application variable would hold your changes. Any values held in an Application variable reset to their default values after your application quits. |
Saved Variables | Saved variables are like Application variables, but they persist even after your application quits. You can use a Saved variable as a simple but powerful save system. Unity stores Saved variables in its PlayerPrefs , and they don't refer to Unity objects, like GameObjects and components. For more information on PlayerPrefs , see PlayerPrefs in the Unity User Manual Scripting Reference. |
Note
You can still access the Blackboard and create new variables with a State Graph open in the Graph window, but you can't add a variable node and use it inside a State Graph.
For Saved variables, there are two additional tabs on the Blackboard: Initial and Saved:
Values defined in the Initial tab apply to all new instances of your application as default values.
Values defined in the Saved tab are the last modified values for those variables, based on when you last ran your application. You can edit them manually, or delete the values to reset them to the values defined in the Initial tab.