Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

StateMachine.Connect

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public ITransition Connect(IState fromState, IState toState);

Parameters

Parameter Description
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

ITransition The 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.

Description

Creates a transition from one state to another.

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.

Additional resources: StateMachine.GetTransitions, StateMachine.Disconnect

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

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