Visual State Machine
  • Welcome
  • FAQ
  • Get Started
    • Setup
    • How to Create a State Machine
    • How to Open the Graph Editor
    • How to Create a State
    • About State IDs and Renaming States
    • How to Duplicate States
    • How to Set the Entry State
    • How to Create a Transition
    • About Transition IDs and Renaming Transitions
    • About Transition Labels
    • How to Trigger a Transition
    • How to Trigger a Transition by Label
    • How to Listen to Graph Events
  • Advanced
    • Execution Order
    • Listening to Events from Script
    • Writing Custom State Behaviours
    • Extending VSM
Powered by GitBook
On this page
  • UI Button OnClick Example
  • Script Example

Was this helpful?

  1. Get Started

How to Trigger a Transition

PreviousAbout Transition LabelsNextHow to Trigger a Transition by Label

Last updated 2 years ago

Was this helpful?

To trigger a transition in a State Machine, call the Trigger method of the State Machine and pass the ID of the transition you want to trigger.

Example: Imagine you have the following State Machine and want to trigger the transition with the ID Show Settings.

The following two examples will show how you could achieve this with an UI Button in the inspector or from inside a script.

UI Button OnClick Example

Script Example

This script will trigger the transition with name Show Settings on the State Machine set in the inspector when the ā€œSā€ key is down.

using Ilumisoft.VisualStateMachine; 
using UnityEngine; 
public class TriggerExample : MonoBehaviour 
{ 
    //Reference to the state machine set via the inspector 
    [SerializeField] StateMachine stateMachine = null; 
    
    private void Update() 
    { 
        if(Input.GetKeyDown(KeyCode.S)) 
        { 
            stateMachine.Trigger("Show Settings"); 
        } 
    } 
}