- Home /
StateMachineBehaviour: Where should I set up references?
When you write a normal MonoBehavior, you put references to other objects in Start(), like this:
public class Foo : MonoBehaviour {
RigidBody2d rb;
void Start() {
rb = GetComponentInParent<Rigidbody2D>();
}
}
What can I do if I want to access that parent's Rigidbody2D in a StateMachineBehavior instead?
Right now I have this:
Rigidbody2D rb;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
if (rb == null) {
rb = animator.gameObject.GetComponentInParent<Rigidbody2D>();
}
}
This works, but I have to wait until the state is triggered to set up the reference. And it will do the checks every time. Is there a way to do it on scene start like a MonoBehaviour does?
Originally I tried putting the same code in OnStateMachineEnter, but it was never called. I read in the docs that it won't be called for sub-state machines, but I am not using those - this machine begins running on scene play.
Answer by ericbegue · Jun 09, 2016 at 07:10 PM
StateMachineBehaviour inherits from ScriptableObject which has the OnEnable function. You could use this function to do the initialization.
See: https://docs.unity3d.com/ScriptReference/ScriptableObject.html
I think you will need to do it manually. Get all behaviors from animator and set whatever references you want.
Answer by olejuer · Apr 14, 2020 at 07:12 AM
I have built a utility class that tries to solve this problem. You can inherit from this class instead of StateMachineBehaviour and you will have access to the Animator and Context properties after the state has been entered once. The Context property is a reference to a component that has to be attached to the gameObject (an exception is thrown otherwise). You can inherit from StateMachineBehaviour, if you have no custom script attached.
As for OnStateMachineEnter: this is called when you change state machines, meaning entering a sub-state machine or returning from one. It will not be called when changing states within a state machine. https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.OnStateMachineEnter.html
As for OnEnable/OnDisable, those do not give you any way to access the Animator component that is driving the state, which is somewhat annoying.
/// <summary>
/// A <see cref="StateMachineBehaviour"/> with a context MonoBehaviour.
/// </summary>
/// <typeparam name="T">Type of the context</typeparam>
public class StateMachineBehaviour<T> : StateMachineBehaviour where T : Component
{
/// <summary>
/// The context MonoBehaviour owning the state machine.
/// </summary>
protected T Context { get; private set; }
protected Animator Animator { get; private set; }
/// <summary>
/// The transform of the owning GameObject.
/// </summary>
protected Transform Transform { get; private set; }
private bool _initialized;
public sealed override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (!_initialized)
{
Animator = animator;
Context = animator.GetComponentInParent<T>();
if (Context == null)
throw new InvalidOperationException(
$"State machine behaviour needs sibling/parent component of type {typeof(T)}");
Transform = Context.transform;
_initialized = true;
OnInitialize(animator, stateInfo, layerIndex);
}
OnStateEntered(animator, stateInfo, layerIndex);
}
/// <summary>
/// Initialize is called when the state is entered for the first time. Called before <see cref="OnStateEntered"/>
/// </summary>
protected virtual void OnInitialize(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{ }
/// <summary>
/// Called every time the state is entered.
/// </summary>
protected virtual void OnStateEntered(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{ }
}
Your answer
Follow this Question
Related Questions
Mecanim and script behaviour issue 1 Answer
Is there a way to get a StateMachineBehaviour on a specific State 2 Answers
Mecanim - State Behavior - Select motion clip to play at runtime 0 Answers
Where variables from StateMachineBehaviour are stored ? 0 Answers
Transition to multiple states from Entry Node in Unity 5 1 Answer