- Home /
Question by
JoltenDev · Oct 23, 2021 at 06:20 AM ·
scripting problemgameobjectreferenceactivestate-machine
GameObject is not activating from StateMachineBehavior script
I'm trying to reference "Hitbox" GameObject that's in the scene and activate it when the animation begins. I run into this error though, "Object reference not set to an instance of an object." I'm not sure why this happens and I have no idea how to fix it. Please help!
private GameObject hitbox;
private GameObject hand;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
hitbox = GameObject.Find("Hitbox");
hand = GameObject.Find("LeftHand");
hitbox.SetActive(true);
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
hitbox.transform.localScale = new Vector3(1, 1, 1.2f);
hitbox.transform.position = new Vector3(hand.transform.position.x, hand.transform.position.y, hand.transform.position.z);
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
hitbox.SetActive(false);
}
// OnStateMove is called right after Animator.OnAnimatorMove()
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that processes and affects root motion
//}
// OnStateIK is called right after Animator.OnAnimatorIK()
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that sets up animation IK (inverse kinematics)
//}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613044519im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding all GameObjects with the same tag when some of them are inactive? 4 Answers
Duplicating Component with Some Scene References in Serialized Scriptable Object Class Reference 0 Answers
Stumped on object not set to an instance of an object, even though it is!? 1 Answer
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers