Search Unity

Get AnimatorState from StateMachineBehaviour

Discussion in 'Scripting' started by Mish, Oct 25, 2016.

  1. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    I have a custom editor script for a StateMachineBehaviour where I can create data I want to use at runtime (OnStateEnter etc.). I store the data in a ScriptableObject, which I can load from at runtime.

    However, in order to retrieve the data at runtime, I need to know which AnimatorState it is associated with. Is there any way go get the name of the AnimatorState which the StateMachineBehaviour is attached to from an editor script?
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    I know this is an old thread but its what I found when searching for the same question. In my case I wanted my StateMachineBehaviours to know what type of clip the AnimatorState was using, so I needed it during the editor

    turns out you'll want to refer the static method AnimatorController.FindStateMachineBehaviourContext(stateMachineBehaviourInstance).

    took me longer than it should to track that down. (was about to resort to reflection).

    but now I got my editor with a nice preview of the clip directly in the state machine behaviour