# StateMachine

> Represents the core definition of a state machine and defines its behavior.

## Definition

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

```csharp
public class StateMachine
```

## Remarks

`StateMachine` serves as the central entry point for:

* Lifecycle management (via [StateMachine.OnEnable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/onenable.md), [StateMachine.OnDisable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/ondisable.md))

* Change tracking (via [StateMachine.OnStateMachineChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/onstatemachinechanged.md))

* Access to states and variables

To register a state machine type and associate it with a custom file extension and configuration options, apply the [StateMachineAttribute](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachineattribute.md) to your custom `StateMachine` class.

You can further control the state machine's behavior using the [StateMachineOptions](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachineoptions.md) enum, which defines traits.

Use the [StateMachineDatabase](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachinedatabase.md) utility class to create, load, and save state machine assets in the Unity Editor. State machines are serialized assets. You can create them through the editor UI with [StateMachineDatabase.PromptInProjectBrowserToCreateNewAsset](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachinedatabase/promptinprojectbrowsertocreatenewasset.md) or load them from disk with [StateMachineDatabase.LoadStateMachine](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachinedatabase/loadstatemachine.md).

## Properties

| Property                                                                                                                   | Description                                                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| [AssetGuid](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/assetguid.md)                           | The `GUID` of the asset file associated with this state machine.                                                                  |
| [AvailableVariableTypes](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/availablevariabletypes.md) | The set of types offered in the blackboard for variable creation.                                                                 |
| [ID](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/id.md)                                         | The globally unique identifier for this state machine.                                                                            |
| [Name](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/name.md)                                     | The name of the state machine.                                                                                                    |
| [StateCount](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/statecount.md)                         | The number of [IState](/engine/6000.7/script-reference/unity/graphtoolkit/editor/istate.md)s in the state machine.                |
| [VariableCount](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/variablecount.md)                   | The number of [IVariable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/ivariable.md)s declared in the state machine. |

## Methods

| Method                                                                                                                               | Description                                                                                                                                 |
| ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| [AddState](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/addstate.md)                                       | Adds a state to the state machine.                                                                                                          |
| [BuildAvailableVariableTypes](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/buildavailablevariabletypes.md) | Builds the set of variable types offered in the blackboard for variable creation.                                                           |
| [Connect](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/connect.md)                                         | Creates a transition from one state to another.                                                                                             |
| [CreateVariable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/createvariable.md)                           | Creates and adds a new variable to the state machine.                                                                                       |
| [Disconnect](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/disconnect.md)                                   | Removes all transitions that go from one state to another.                                                                                  |
| [GetState](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/getstate.md)                                       | Retrieves a state in the state machine by its index.                                                                                        |
| [GetStates](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/getstates.md)                                     | Retrieves all states in the state machine.                                                                                                  |
| [GetTransitions](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/gettransitions.md)                           | Retrieves the transitions that connect one state to another.                                                                                |
| [GetVariable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/getvariable.md)                                 | Retrieves a variable declared in the state machine by index.                                                                                |
| [GetVariables](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/getvariables.md)                               | Retrieves all variables declared in the state machine.                                                                                      |
| [OnDisable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/ondisable.md)                                     | Called when the state machine is unloaded, or goes out of scope in the editor.                                                              |
| [OnEnable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/onenable.md)                                       | Called when the state machine is created or loaded in the editor.                                                                           |
| [OnStateMachineChanged](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/onstatemachinechanged.md)             | Called after the state machine has changed.                                                                                                 |
| [RemoveState](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/removestate.md)                                 | Removes a state from the state machine.                                                                                                     |
| [RemoveVariable](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/removevariable.md)                           | Removes a variable from the state machine.                                                                                                  |
| [UndoBeginRecordStateMachine](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/undobeginrecordstatemachine.md) | Signals the beginning of an undoable operation.                                                                                             |
| [UndoEndRecordStateMachine](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/undoendrecordstatemachine.md)     | Signals the end of an undoable operation. Sends the undo data to the editor undo system and refreshes the graph view with the changes made. |
