Hi,
The event is sending and I am seeing the debug lines perfectly but I have 1 yellow error and 1 red that I am having trouble understanding. Basically I am trying to use the state machine behavior to listen & send an event when it starts to play an shoot animation and when it ends.
This is my state machine behavior script:
credit goes to: Reddit - Dive into anything
using UnityEngine;
using System.Collections;
public class StateMachineEvent: StateMachineBehaviour
{
public delegate void StateEventHandler(Animator animator, AnimatorStateInfo stateInfo, int layerIndex);
public event StateEventHandler OnStateEntered;
public event StateEventHandler OnStateExited;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
base.OnStateEnter(animator, stateInfo, layerIndex);
if (OnStateEntered != null)
{
OnStateEntered (animator, stateInfo, layerIndex);
}
}
public virtual void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
base.OnStateExit(animator, stateInfo, layerIndex);
if (OnStateExited != null)
{
OnStateExited (animator, stateInfo, layerIndex);
}
}
}
This is my the script I have on my gameobject:
using UnityEngine;
using System.Collections;
public class AIController: MonoBehaviour
{
private Animator animator;
void Awake()
{
animator = GetComponent<Animator>();
}
void OnEnable()
{
animator.GetBehaviour<StateMachineEvent>().OnStateEntered += RifleOneShotStart;
animator.GetBehaviour<StateMachineEvent>().OnStateExited += RifleOneShotEnd;
}
void RifleOneShotStart(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Debug.Log("Rifle one shot - animation START");
}
void RifleOneShotEnd(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
Debug.Log("Rifle one shot - animation END");
}
void OnDisable()
{
animator.GetBehaviour<StateMachineEvent>().OnStateEntered -= RifleOneShotStart;
animator.GetBehaviour<StateMachineEvent>().OnStateExited -= RifleOneShotEnd;
}
}
This is the yellow error that shows up when I am in the editor:
Assets/Misc/StateMachineEvent.cs(21,29): warning CS0114: `StateMachineEvent.OnStateExit(UnityEngine.Animator, UnityEngine.AnimatorStateInfo, int)' hides inherited member `UnityEngine.StateMachineBehaviour.OnStateExit(UnityEngine.Animator, UnityEngine.AnimatorStateInfo, int)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword
This is the red error that happens only when I hit stop. Not during play mode ever, only shows up after I press stop:
NullReferenceException: Object reference not set to an instance of an object
AIController.OnDisable () (at Assets/Misc/AIController.cs:31)
Can anyone explain these errors in layman’s terms?
Any help is appreciated, thank you.