# CreateVariable

> Creates and adds a new variable to the graph.

## Definition

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

## CreateVariable(string, Type, Object, VariableKind)

Creates and adds a new variable to the graph.

```csharp
public IVariable CreateVariable(string name, Type valueType, object defaultValue = null, VariableKind kind = VariableKind.Local)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): The name of the variable.**** (\[Type]\(https\://learn.microsoft.com/dotnet/api/system.type)): The data type of the variable.**** (\[Object]\(https\://learn.microsoft.com/dotnet/api/system.object)): The default value. Must be compatible with `valueType` and work with Unity serialization rules for [SerializeField](/engine/6000.6/script-reference/unityengine/serializefield.md).**** (\[VariableKind]\(/engine/6000.6/script-reference/unity/graphtoolkit/editor/variablekind)): The kind of variable, defined by [VariableKind](/engine/6000.6/script-reference/unity/graphtoolkit/editor/variablekind.md).

### Returns

| Type                                                                                | Description                 |
| ----------------------------------------------------------------------------------- | --------------------------- |
| [IVariable](/engine/6000.6/script-reference/unity/graphtoolkit/editor/ivariable.md) | The newly created variable. |

### Remarks

If successful, also adds the node's type to the graph's list of supported types. Enclose this method with [Graph.UndoBeginRecordGraph](/engine/6000.6/script-reference/unity/graphtoolkit/editor/graph/undobeginrecordgraph.md) and [Graph.UndoEndRecordGraph](/engine/6000.6/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.

## CreateVariable\<T>(string, T, VariableKind)

Creates and adds a new variable to the graph.

```csharp
public IVariable CreateVariable<T>(string name, T defaultValue = default, VariableKind kind = VariableKind.Local)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)): The name of the variable.**** (T): The default value. Must be compatible with `T` and work with Unity serialization rules for [SerializeField](/engine/6000.6/script-reference/unityengine/serializefield.md).**** (\[VariableKind]\(/engine/6000.6/script-reference/unity/graphtoolkit/editor/variablekind)): The kind of variable, defined by [VariableKind](/engine/6000.6/script-reference/unity/graphtoolkit/editor/variablekind.md).

### Returns

| Type                                                                                | Description                 |
| ----------------------------------------------------------------------------------- | --------------------------- |
| [IVariable](/engine/6000.6/script-reference/unity/graphtoolkit/editor/ivariable.md) | The newly created variable. |

### Remarks

If successful, also adds the variable's type to the graph's list of supported types. Enclose this method with [Graph.UndoBeginRecordGraph](/engine/6000.6/script-reference/unity/graphtoolkit/editor/graph/undobeginrecordgraph.md) and [Graph.UndoEndRecordGraph](/engine/6000.6/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.
