Home »API Reference»Accessing a PlayMakerFSM in Scripts
Index

Accessing a PlayMakerFSM in Scripts

There are a few ways to reference a PlayMakerFsm in your scripts:

1. Add a public PlayMakerFSM variable and link to the component.

C# example:


using UnityEngine;
using HutongGames.PlayMaker;

public class MyBehaviour : MonoBehaviour
{
    public PlayMakerFSM fsm;

    void Update()
    {
        // getting fsm variables by name
        FsmFloat floatVariable = fsm.FsmVariables.GetFsmFloat("myFloat");

        // setting fsm variable value
        floatVariable.Value = 0.5f;

        // sending an event
        fsm.SendEvent("myEvent");
    }
}

2. Use GetComponent to get an PlayMakerFSM Component on a GameObject.

Javascript example:


var LivesFSM : PlayMakerFSM;

//assign the FSM on the Lives obj
LivesFSM = gameObject.FindWithTag("Lives").GetComponent.<PlayMakerFSM>();
        
var LivesRemaining : int;
LivesRemaining= LivesFSM.FsmVariables.GetFsmInt("Remaining_Lives").Value;

Another Javascript example:

3. Use GetComponents and the FsmName property to find a specific FSM on a GameObject that has multiple FSMs

Get all PlayMakerFSM components on a GameObject using GetComponents<PlayMakerFSM>(), then loop through them and find the one with the right FsmName.

EDIT: There is now a static utility method in PlayMakerFSM to make this easier:


 public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)