Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Connect(IState, IState)

Creates a transition from one state to another.
Read time 2 minutesLast updated 6 days ago

Definition

public ITransition Connect(IState fromState, IState toState)

Parameters

fromState

The state the transition originates from. Must belong to this state machine.

toState

The state the transition goes to. Must belong to this state machine.

Returns

Type

Description

ITransitionThe ITransition the connection was made on. If
fromState
and
toState
are the same state, the returned transition is a self transition (an ISelfTransition), 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 to inspect the rules of a transition.
Enclose this method with StateMachine.UndoBeginRecordStateMachine and StateMachine.UndoEndRecordStateMachine 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.
The following example connects two states, then gives the second state a self transition with two rules.

Examples

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();}