Can't trigger animation on Collider2D
I can't figure out why this doesn't work.
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("animazione2", true);
}
}
private void OnTriggerExit(Collider other)
{
if(other.CompareTag("Player"))
{
MyAnimationController.SetBool("animazione2", false);
}
}
}
My trigger collider2D is on the prefabs (tagged as "Spine") of a spawner (named "SpineSxSpawner"). I attached the script to the prefabs, hit apply and deleted the prefabs from the hierarchy. "Player" is the tag of the GameObject which gets animated, by default it has "AvatarAnimation" as animation state, but I created a new state called "animazioneVienePunto" to which the default state is supposed to transition when the "animation2" parameter is true; it is supposed to transition the other way around when the "animation2" parameter is false.
Basically what I want to do is to change animation of the avatar from kind of an Idle animation to sort of "player gets hurt" animation, when the obstacles (tagged as "Spine") hit the avatar (tagged as "Player") and then go back to Idle animation when the collider2D of both object exit from a state of collision.
I already debugged the collider2D, the hit is correctly detected by the console. Also, I don't know why but if I set the controller as "Avatar" in My Animator Controller (on the inspector, inside this script I applied to the prefabs tagged as "Spine") and I hit Apply All, then delete the prefabs from the hierarchy, when I select them from the assets, this setting of "Avatar" in My Animator Controller disappears and the field appears to be empty.
EDIT
I found out that the problem was that I used 3D functions instead of 2D ones, I think that the animation is called now. What I can't figure out is why my prefabs array (the stuff tagged as "Spine") is not keeping the variable I assign under "MyAnimationController" in the inspector. I think that's why I get this error but I don't know how to fix it:
UnassignedReferenceException: The variable MyAnimationController of playMyAnimation has not been assigned. You probably need to assign the MyAnimationController variable of the playMyAnimation script in the inspector.
UnityEngine.Animator.SetBool (System.String name, System.Boolean value) (at /Users/builduser/buildslave/unity/build/Runtime/Animation/ScriptBindings/Animator.bindings.cs:343) playMyAnimation.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/playMyAnimation.cs:13)
Your answer
Follow this Question
Related Questions
Check for collision while animating 0 Answers
Mecanim animation playing once on button press 2 Answers
How to have a trigger activate animation on another game object. 2 Answers
I want to freeze fp player , animation then starts , then unfreeze the character . 0 Answers
Play Animation OnTriggerEnter2D 1 Answer