trying to trigger animation event and getting NullReferenceException

as you can tell im new and really trying here for hours but im at a loss-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayMyAnimation : MonoBehaviour
{
    [SerializeField] private Animator myAnimationController;

    private void OnTriggerEnter(Collider other)
    {
        if (other.comparetag("Player"))
        {
            myAnimationController.SetBool("playbikemove", true);
        }
    }


    private void OnTriggerExit(Collider other)
    {
        if (other.comparetag("Player"))
        {
            myAnimationController.SetBool("playbikemove", false);
        }
    }
}

i want to walk into a box collider and have it trigger the animation, when i walk out it should stop the animation. but its not working at all, i followed a tutorial and it works for others so i dont know.

First thing to check is if “myAnimationController” has been assigned in the inspector.

Next thing to check is if the functions are even being called. You could use a Debug.Log to print something in the console.

The OnTriggerEnter/Exit/Stay functions require a rigidbody to work. Ensure that your player has a rigidbody component attached to it.

On a side note, does this code compile? Isn’t it supposed to be “CompareTag”.

1 Like

Add myAnimationController = GetComponent.() if the Animator is attached to the gameobject.

after adding rigidbody and changing comparetag it seems to be getting closer but i now get this error-
“cannot add component because script class cannot be found. make sure the file name and class match and check for compile errors.”

they do match and everything looks good in visual studio.

Locate the .cs file containing this class and rename it to “PlayMyAnimation”.