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.
StateMachineBehaviour inherits from ScriptableObject which has the OnEnable function. You could use this function to do the initialization.
See: Unity - Scripting API: ScriptableObject
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. Unity - Scripting API: StateMachineBehaviour.OnStateMachineEnter
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)
{ }
}