docs.unity3d.com
  • Manual
  • Scripting API
  • Changelog
  • License
Show / Hide Table of Contents
  • About Visual Scripting
    • Configure project settings
      • Add or remove available nodes
      • Add or remove types
      • Create or restore a backup
    • Choose a control scheme
    • Configure your preferences
    • Update Visual Scripting
    • Version control systems
    • Use Visual Scripting with Unity Cloud Build
  • Basic concepts in Visual Scripting
    • The interface
    • Nodes
    • Graphs
      • Subgraphs and State Units
      • Transitions
    • Script Machines and State Machines
    • Object types
      • Custom types
    • Variables
  • Develop application logic with Script Graphs
    • Create a new graph file
      • Create a new blank graph with the Project window
      • Create a new unassigned graph with the empty graph creation flow
      • Create and assign a graph to an existing GameObject
      • Create and assign a graph to a new GameObject
      • Create a graph on a Script Machine or State Machine
    • Attach a graph file to a Script Machine or State Machine
    • Open a graph file
      • Add a node to a Script Graph
      • Connect nodes in a Script Graph
      • Create and add a variable to a Script Graph
      • Create node groups
      • Add comments to a graph
    • Add a Subgraph to a Script Graph
      • Add a Trigger or Data port to a Script Graph
    • Add a State Unit to a Script Graph
    • Custom Events
      • Add a Custom Event node
      • Add a Trigger Custom Event node
    • Capture user input in an application
      • Capture input using the Input Manager
      • Add and configure a Player Input component
      • Capture input using the Input System package
    • Use relations to debug
      • Predictive and live debugging
      • Working with debug messages
    • Live edit
      • Live edit during runtime
  • Develop logic transitions with state graphs
    • Create a new state
    • Create a transition between states
  • Advanced customization and development
    • Refactor a C# script with Visual Scripting
      • Add the RenamedFrom attribute to a C# script
    • Custom C# nodes
      • Create a new simple Custom C# node
      • Add ports to your Custom C# node
      • Add logic to your Custom C# node
      • Add relations to your Custom C# node
      • Add documentation to your Custom C# node
      • Custom C# node attributes reference
    • Create a Custom Scripting Event node
      • Create a Custom Scripting Event Sender node
      • Trigger a Custom Scripting Event from a C# script
      • Listen to a Custom Scripting Event from a C# script
    • Use a custom type
      • Add the Inspectable attribute to a custom type
      • Create a custom PropertyDrawer for a custom type
  • Node reference
    • This node
    • Control node
    • Time node
    • Events
      • Event nodes
      • Input Event nodes
        • On Input System Event Button
        • On Input System Event Float
        • On Input System Event Vector 2
        • On Button Input
        • On Keyboard Input
        • On Mouse Down
        • On Mouse Drag
        • On Mouse Enter
        • On Mouse Exit
        • On Mouse Input
        • On Mouse Over
        • On Mouse Up As Button
        • On Mouse Up
    • Variable node
    • Nulls node
    • Formula node
    • Nesting
      • Input node
      • Output node
      • State Unit node
      • Subgraph node
    • Script graph nodes
    • State graph nodes
  • Developer's guide
  • Known Issues

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 or string .
  • A value for the variable, such as 1 or cat.

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:
A. The Flow variable doesn’t have a direct or indirect connection to the nodes where you want to use its value. The node where the variable is defined must be a part of the logical flow where you want to use its value.
B. The Flow variable hasn’t been set before Visual Scripting tries to run any logic that needs its value. The node where the variable is defined must come before any other logic in your graph.
You can't create a Flow variable from the Blackboard - you can create one with a Set Variable node and set the Scope to Flow.
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, refer to 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.

An image that displays a comparison between the Initial and Saved tabs for a set of defined Saved variables. The values for the Saved variables are different across the Initial and Saved tabs.

In This Article
Back to top
Copyright © 2025 Unity Technologies — Trademarks and terms of use
  • Legal
  • Privacy Policy
  • Cookie Policy
  • Do Not Sell or Share My Personal Information