Class Variables
Component that handles the variables that you can use in the Script Graphs of your project.
Inherited Members
Namespace: Unity.VisualScripting
Assembly: Unity.VisualScripting.Core.dll
Syntax
[AddComponentMenu("Visual Scripting/Variables")]
[IncludeInSettings(false)]
public class Variables : LudiqBehaviour, ISerializationCallbackReceiver, IAotStubbable
Remarks
This component is automatically added to a GameObject when a ScriptMachine is added, or when SceneVariables (Script) are created using the “Visual Scripting Scene Variables” menu in the Hierarchy.
The Variables component manages the object variables for any GameObject that you add it to. You can access these variables from the Object Variables section of the Blackboard of any ScriptMachine associated with the GameObject.
The Variables component also manages the scene variables for any “VisualScripting SceneVariables” GameObject that you add it to. You can access these scene variables from the Scene Variables section of the Blackboard of any ScriptMachine you add to this same scene.
Note: It's best not to add a ScriptMachine component to a GameObject that also includes the SceneVariables (Script). If you do this, the Blackboard displays Scene Variables that are also present in Object Variables and this can cause confusion.
For more information on how to interact with Variables, refer to the User Manual.
Examples
The following example shows how to programmatically change the value of a variable used in a Visual Scripting graph. Every time you press the Space key, we double the value of the velocity variable. Note: You can try this example in a Script Graph.
public class PlayerController : MonoBehaviour
{
VariableDeclaration m_Velocity;
void Start()
{
var variables = GetComponent<Variables>();
m_Velocity = variables.declarations.GetDeclaration("velocity");
}
void Update()
{
if (Input.GetKeyDown("space"))
{
var currentVelocity = (float)m_Velocity.value;
m_Velocity.value = currentVelocity * 2f;
}
}
}
Properties
ActiveScene
Retrieve a collection of the scene variables of the active scene.
Declaration
public static VariableDeclarations ActiveScene { get; }
Property Value
| Type | Description |
|---|---|
| VariableDeclarations |
Application
Retrieve a collection of the application variables.
Declaration
public static VariableDeclarations Application { get; }
Property Value
| Type | Description |
|---|---|
| VariableDeclarations |
ExistInActiveScene
Check if there is a SceneVariables component instantiated to find out if the active scene contains scene variables.
Declaration
public static bool ExistInActiveScene { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Returns true if the active scene contains scene variables. Otherwise, returns false.
Saved
Retrieve a collection of the saved variables.
Declaration
public static VariableDeclarations Saved { get; }
Property Value
| Type | Description |
|---|---|
| VariableDeclarations |
declarations
Retrieves a collection of the variables set in the Variables component.
Declaration
[Serialize]
[Inspectable]
public VariableDeclarations declarations { get; }
Property Value
| Type | Description |
|---|---|
| VariableDeclarations |
Methods
ExistInScene(Scene?)
Check if there is a SceneVariables component instantiated to find out if the scene contains scene variables.
Declaration
public static bool ExistInScene(Scene? scene)
Parameters
| Type | Name | Description |
|---|---|---|
| Scene? | scene | A Scene we want to check for scene variables. |
Returns
| Type | Description |
|---|---|
| bool | True if the Scene is not null and contains scene variables. Otherwise, returns false. |
ExistOnObject(Component)
Check if a Variables component exists on the Component's GameObject passed as a parameter.
Declaration
public static bool ExistOnObject(Component component)
Parameters
| Type | Name | Description |
|---|---|---|
| Component | component | A Component for which we want to know if the GameObject has a Variables component. |
Returns
| Type | Description |
|---|---|
| bool | True if the Component's GameObject has a Variables component. Otherwise, returns false. |
ExistOnObject(GameObject)
Check if a Variables component exists on the GameObject passed as a parameter.
Declaration
public static bool ExistOnObject(GameObject go)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | go | The GameObject we want to check for the Variables component. |
Returns
| Type | Description |
|---|---|
| bool | True if the GameObject has a Variables component. Otherwise, returns false. |
Graph(GraphPointer)
Retrieves a collection of graph variables for a given graph.
Declaration
public static VariableDeclarations Graph(GraphPointer pointer)
Parameters
| Type | Name | Description |
|---|---|---|
| GraphPointer | pointer | The reference to a graph. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | If the graph is instantiated, returns the graph variables of that instantiated graph. Otherwise, returns the graph variables from the definition of the given graph (i.e.: from the graph asset definition) |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |
GraphDefinition(GraphPointer)
Retrieves a collection of graph variables from the definition of a graph.
Declaration
public static VariableDeclarations GraphDefinition(GraphPointer pointer)
Parameters
| Type | Name | Description |
|---|---|---|
| GraphPointer | pointer | The reference to a graph. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of graph variables of a given graph. |
GraphDefinition(IGraphWithVariables)
Retrieves a collection of graph variables from the definition of a graph.
Declaration
public static VariableDeclarations GraphDefinition(IGraphWithVariables graph)
Parameters
| Type | Name | Description |
|---|---|---|
| IGraphWithVariables | graph | The reference of a graph |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of graph variables of a given graph. |
GraphInstance(GraphPointer)
Retrieves a collection of graph variables of an instantiated graph.
Declaration
public static VariableDeclarations GraphInstance(GraphPointer pointer)
Parameters
| Type | Name | Description |
|---|---|---|
| GraphPointer | pointer | The reference to a graph. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of graph variables of an instantiated graph. |
Exceptions
| Type | Condition |
|---|---|
| GraphPointerException | If the graph data cannot be read. Which probably means that the graph is not instantiated. |
Object(Component)
Retrieves a collection of the object variables of a given GameObject.
Declaration
public static VariableDeclarations Object(Component component)
Parameters
| Type | Name | Description |
|---|---|---|
| Component | component | The Component whose GameObject's object variables are returned. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of object variables contained in the Variables component of the Component's GameObject. |
Remarks
If the GameObject does not have a Variables component, a Variables component is added to the GameObject and the returned collection is empty
Object(GameObject)
Retrieves a collection of the object variables of a given GameObject.
Declaration
public static VariableDeclarations Object(GameObject go)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | go | The GameObject whose object variables will be returned. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of the object variables contained in the Variables component of the GameObject that was passed as a parameter. |
Remarks
If the GameObject doesn't have a Variables component, it is supplied with one by default and the returned collection is empty.
Scene(Scene?)
Retrieves a collection of scene variables for a given Scene.
Declaration
public static VariableDeclarations Scene(Scene? scene)
Parameters
| Type | Name | Description |
|---|---|---|
| Scene? | scene | The Scene whose scene variables are returned. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of scene variables contained in the Variables component associated with the SceneVariables (Script). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |
Scene(Component)
Retrieves a collection of scene variables for a given Scene.
Declaration
public static VariableDeclarations Scene(Component component)
Parameters
| Type | Name | Description |
|---|---|---|
| Component | component | A Component whose GameObject's Scene's scene variables will be returned. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of scene variables contained in the Variables component associated with the SceneVariables (Script). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If the |
Scene(GameObject)
Retrieves a collection of scene variables for a given Scene.
Declaration
public static VariableDeclarations Scene(GameObject go)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | go | A GameObject whose Scene will be accessed to get its variables. |
Returns
| Type | Description |
|---|---|
| VariableDeclarations | A collection of scene variables contained in the Variables component associated with the SceneVariables (Script). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If the |
ShowData()
Declaration
[ContextMenu("Show Data...")]
protected override void ShowData()