0
\$\begingroup\$

I have the following script on my object:

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

    private void onTriggerEnter(Collider other) {
        if (other.CompareTag("Player")) {
            myAnimationController.SetBool("playSpin2", true);
        }
    } 

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

When the player character is inside the first cube's trigger, my second cube should play a spinning animation. When they leave, the second cube should stop.

But what happens in the game now is, when the character runs into the trigger, it does nothing.

I have configured my trigger object like this:

Unity Inspector Screenshot

I have configured the object that should spin like this:

Unity Inspector Screenshot

And configured the Animation Controller like this:

Animation control graph

Transition into spin

Transition out of spin

And configured my Player object like this:

Unity Inspector for Player

What can I do to make this animation play when the player is inside the trigger?

\$\endgroup\$
0
\$\begingroup\$

The method is called OnTriggerEnter (uppercase O)

What you wrote in your script is onTriggerEnter (lowercase o)

Identifiers in C# are case-sensitive, so these are two completely different functions as far as the compiler/engine is concerned. When it tries to call OnTriggerEnter, it discovers there's no method on your script with exactly that name, so it does nothing.

Please be careful to type your scripts correctly, and proofread your code for typos.

You can find this class of error faster in future by using a breakpoint or Debug.Log to determine whether the method is even being called when you expect. That will let you isolate the problem and find your own answers quickly. Or, if the answer still eludes you, it will at least help you ask a more concise question: all the extra detail you showed with the animator here isn't relevant to the problem, if the onTriggerEnter method is never being called.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.