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

Create and add a variable to a Script Graph

You can create and add a variable to a Script Graph in one of two ways: use the Graph window's Blackboard, or add a node to a graph.

Note

You can't add a Flow variable to a graph from the Blackboard. For more information on Flow variables, see Variables.

Create and add a new variable through the Blackboard

To create a new variable in the Blackboard and add it to a graph:

  1. With a graph open in the Graph window, open the Blackboard.

  2. In the Blackboard, select the scope for the variable you want to add: Graph, Object, Scene, App, or Saved. For more information on variable scopes and when you can use each scope in your graph, see Variables.

  3. In the (New Variable Name) field, enter a name for the new variable.

  4. Do one of the following:

    • Press Enter.
    • Select the Add Variable (+) button.
  5. In the Type list, select the data type for your variable. For more information on types, see Object types.

  6. In the Value field, enter or modify the default value for your variable.

  7. To add the node to your graph, click and drag from the Handle (=) on the variable's definition in the Blackboard into the Graph Editor.

Visual Scripting adds a new Get Variable node for your variable to your graph.

An image of the Visual Scripting Blackboard and Graph Editor. A Saved variable has been dragged from the Blackboard and onto the Graph Editor as a new Get Variable node.

Create and add a new variable through the Graph Editor

To create a Flow variable or another new variable directly in a graph:

  1. With a Script Graph open in the Graph window, right-click on an empty space in the Graph Editor to open the fuzzy finder.

  2. Select the Variables category.

  3. Select the scope for the variable you want to add: Flow, Graph, Object, Scene, Application, or Saved. For more information on variable scopes and when you can use each scope in a graph, see Variables.

  4. Select the Set <Scope> Variable node, where <Scope> is the scope you selected in the previous step. Visual Scripting adds a new Set Variable node to the graph.

  5. In the Name field, enter a name for the new variable.

  6. (Object variables only) In the GameObject field, indicated by a GameObject icon on the node, specify the GameObject where you want to create the variable. Do one of the following:

    • Select the object picker (circle icon) and select a GameObject.
    • Attach a node to the field's data input port that outputs a GameObject.
    • Leave the field as the default value of This to use the GameObject where you attached the Script Graph to a Script Machine.
  7. To set a default value for your variable, connect another node that outputs the value you want to the Set Variable node's data input port.

An image of the Visual Scripting Graph Editor. A new Set Variable node called "sum" has an assigned default value from an Add Inputs node. A Find GameObject node specifies the GameObject where "sum" is set.

Next steps

After you've added a variable to your graph, you can add nodes, create node groups, or add a Subgraph.

You can also add a Sticky Note to add comments to a graph.

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