# Connect(IState, IState)

> Creates a transition from one state to another.

## Definition

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

```csharp
public ITransition Connect(IState fromState, IState toState)
```

### Parameters

**** (\[IState]\(/engine/6000.7/script-reference/unity/graphtoolkit/editor/istate)): The state the transition originates from. Must belong to this state machine.**** (\[IState]\(/engine/6000.7/script-reference/unity/graphtoolkit/editor/istate)): The state the transition goes to. Must belong to this state machine.

### Returns

| Type                                                                                    | Description                                                                                                                                                                                                                                                                                                                                                                                         |
| --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ITransition](/engine/6000.7/script-reference/unity/graphtoolkit/editor/itransition.md) | The [ITransition](/engine/6000.7/script-reference/unity/graphtoolkit/editor/itransition.md) the connection was made on. If `fromState` and `toState` are the same state, the returned transition is a self transition (an [ISelfTransition](/engine/6000.7/script-reference/unity/graphtoolkit/editor/iselftransition.md)), and it is the state's existing self transition when it already has one. |

### Remarks

This is the state machine equivalent of connecting two ports with a wire. Between two different states, a new transition is created on every call, so calling this method twice on the same pair of states produces two distinct transitions, each seeded with a single empty rule.

A state holds a single self transition, so when `fromState` and `toState` are the same state and that state already has a self transition, no second transition is created: a new rule is added to the existing self transition, which is returned. To retrieve the rule that was added, read the last rule of the returned transition. Use [ITransition.GetRules](/engine/6000.7/script-reference/unity/graphtoolkit/editor/itransition/getrules.md) to inspect the rules of a transition.

Enclose this method with [StateMachine.UndoBeginRecordStateMachine](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/undobeginrecordstatemachine.md) and [StateMachine.UndoEndRecordStateMachine](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/undoendrecordstatemachine.md) to add this operation to the undo stack and to update the graph view with the changes. Throws ArgumentNullException when `fromState` or `toState` is `null`. Throws ArgumentException when either state does not belong to this state machine.

Additional Resources: [StateMachine.GetTransitions](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/gettransitions.md), [StateMachine.Disconnect](/engine/6000.7/script-reference/unity/graphtoolkit/editor/statemachine/disconnect.md)

The following example connects two states, then gives the second state a self transition with two rules.

### Examples

```csharp
void BuildTransitions(StateMachine stateMachine, IState idle, IState patrol)
{
    stateMachine.UndoBeginRecordStateMachine("Build transitions");

    // Idle -> Patrol, a new transition holding one rule.
    stateMachine.Connect(idle, patrol);

    // Patrol -> Patrol, a self transition holding one rule.
    var patrolLoop = stateMachine.Connect(patrol, patrol);

    // Patrol already has a self transition, so this adds a second rule to it rather than
    // creating another transition: it returns patrolLoop, whose RuleCount is now 2.
    stateMachine.Connect(patrol, patrol);

    stateMachine.UndoEndRecordStateMachine();
}
```
