Get animator controller for a drawer on a statemachinebehaviour property.

I’m trying to make a property drawer that includes a popup list of all the parameters in a given Animator Controller.

Getting the Animator Controller from the gameObject is pretty easy:

animCntrl = (AnimatorController)Selection.activeGameObject.GetComponent<Animator>().runtimeAnimatorController;

but now I want to put that same property on a StateMachineBehaviour, so I need a way to get the currently open Animator controller.

Thanks for your time.

1 Like

StateMachineBehaviors reside on the animator component itself, not a on a game object in the scene like MonoBehaviors. The side effect is that it’s unaware of objects outside that sope, like the hiearchy (aka the scene). So pretty much everything usefull.

Your only link to the outside world is through the animator property you receive as a parameter on the OnState functions.

So if for example you want to run a function from a script on the game object the animator is attached to you do something like this:

using UnityEngine;

public class StateMachineGobalVariables : StateMachineBehaviour {

public scriptClassName script;

    override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        script = animator.gameObject.GetComponent<scriptClassName> ();
        script.PublicFunctionName ();
    }
}

Unfortunatly you end up using GetComponent way more than you would like.

You can also use:

  • GameObject.Find
  • GameObject.FindGameObjectWithTag
  • GameObject.FindGameObjectsWithTag
  • GameObject.FindWithTag

But we both feel the same about using those.

Personally I’m going to try setting a few global variables on my idle state (default state on startup) using a StateMachineBehavior too see if that can help prevent using GetComponent. Just haven’t gotten around to trying it yet. Hopefully behaviors inside the state machine can see each other otherwise that plan is dead.

For more details on StateMachineBehaviors follow this link.

Good luck.