# AddVariableNode

> Creates and adds a new variable node referencing an existing variable.

## Definition

* **Type:** Method
* **Namespace:** [Unity.GraphToolkit.Editor](/engine/6000.7/script-reference/unity/graphtoolkit/editor.md)
* **Assembly:** UnityEditor

## AddVariableNode(IVariable, Vector2)

Creates and adds a new variable node referencing an existing variable.

```csharp
public IVariableNode AddVariableNode(IVariable variable, Vector2 position)
```

### Parameters

**** (\[IVariable]\(/engine/6000.7/script-reference/unity/graphtoolkit/editor/ivariable)): Variable to reference. Must belong to this graph.**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): Position of the node on the graph canvas.

### Returns

| Type                                                                                        | Description                      |
| ------------------------------------------------------------------------------------------- | -------------------------------- |
| [IVariableNode](/engine/6000.7/script-reference/unity/graphtoolkit/editor/ivariablenode.md) | The newly created variable node. |

### Remarks

The created node is in [VariableNodeMode.Get](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablenodemode/get.md) mode. To create a node that can write to the variable, use [Graph.AddVariableNode](/engine/6000.7/script-reference/unity/graphtoolkit/editor/graph/addvariablenode.md) and pass [VariableNodeMode.Set](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablenodemode/set.md). Set mode is only available for variables of kind [VariableKind.Local](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablekind/local.md).

Enclose this method with [Graph.UndoBeginRecordGraph](/engine/6000.7/script-reference/unity/graphtoolkit/editor/graph/undobeginrecordgraph.md) and [Graph.UndoEndRecordGraph](/engine/6000.7/script-reference/unity/graphtoolkit/editor/graph/undoendrecordgraph.md) to add this operation to the undo stack and to update the graph view with the changes.

### Examples

```csharp
IVariable health = graph.CreateVariable<int>("Health");

graph.UndoBeginRecordGraph("Add Variable Node");
IVariableNode node = graph.AddVariableNode(health, new Vector2(100, 100));
graph.UndoEndRecordGraph();

// node.Mode == VariableNodeMode.Get
// Health is a local variable, so the node has an output port that provides its current value.
```

## AddVariableNode(IVariable, Vector2, VariableNodeMode)

Creates and adds a new variable node with the specified mode, referencing an existing variable.

```csharp
public IVariableNode AddVariableNode(IVariable variable, Vector2 position, VariableNodeMode mode)
```

### Parameters

**** (\[IVariable]\(/engine/6000.7/script-reference/unity/graphtoolkit/editor/ivariable)): Variable to reference. Must belong to this graph.**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): Position of the node on the graph canvas.**** (\[VariableNodeMode]\(/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablenodemode)): Mode of the variable node.

### Returns

| Type                                                                                        | Description                      |
| ------------------------------------------------------------------------------------------- | -------------------------------- |
| [IVariableNode](/engine/6000.7/script-reference/unity/graphtoolkit/editor/ivariablenode.md) | The newly created variable node. |

### Remarks

The `mode` parameter determines the ports the node exposes. Refer to [VariableNodeMode](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablenodemode.md) for details. [VariableNodeMode.Set](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablenodemode/set.md) is only supported for variables of kind [VariableKind.Local](/engine/6000.7/script-reference/unity/graphtoolkit/editor/variablekind/local.md).

To change the mode of an existing variable node, use the **Allow to set value in graph** checkbox in the node's inspector.

Enclose this method with [Graph.UndoBeginRecordGraph](/engine/6000.7/script-reference/unity/graphtoolkit/editor/graph/undobeginrecordgraph.md) and [Graph.UndoEndRecordGraph](/engine/6000.7/script-reference/unity/graphtoolkit/editor/graph/undoendrecordgraph.md) to add this operation to the undo stack and to update the graph view with the changes.

### Examples

```csharp
// Local variables support both Get and Set modes.
IVariable speed = graph.CreateVariable<float>("Speed");

graph.UndoBeginRecordGraph("Add Set Variable Node");
IVariableNode setNode = graph.AddVariableNode(speed, new Vector2(100, 100), VariableNodeMode.Set);
graph.UndoEndRecordGraph();

// setNode.Mode == VariableNodeMode.Set
// The node has an extra input port for writing the value of Speed in the graph.
```
