Enabled an animator bool of another gameobject, but how to disable?

Everyone,

Need a little script assistance. I have a collider triggering a “taking damage” animator bool of a target/enemy. While it’s working (at triggering the bool to true), I can’t figure out how to return it to false now. Maybe it’s really simple, but my current “simple” method just does not work.

public class AttackDetection : MonoBehaviour
{
 public int damage; // declare an interger called damage, do not assign value yet


private void OnCollisionEnter2D(Collision2D collision) // call Collision2D as collision
    {
        if (collision.collider.gameObject.tag == "Weak Point") // if Collision2D occurs on collider of gameobject with a tag "weak point"

        {
            Animator animator = collision.gameObject.GetComponent<Animator>(); // declare "animator" henchforth as "collided gamobject component of animator"
           
             if (animator != null)                 // animator is definitely null?
            {

                animator.SetBool("TakingDamage", true);  // animator state of our above declared "animator" called "TakingDamage" will be triggered to true

            }

            else animator.SetBool("TakingDamage", false); // otherwise the animator state of "Taking Damage" should be set to false

     collision.gameObject.GetComponent<MonsterHealth>().TakeDamage(damage); // perform the "Take Damage" function inside the Monster Health script

        }

I think you are almost there but there is one minor crossed wire:

line 14 is checking if the animator is null…

don’t you want to check something ELSE to decide if you are setting TakingDamage to false / true??

In that case on the OnCollisionExit2D() perhaps you would call setting it false, while still setting it to true on ENTER??

Still though, good to be checking animator for non-null-ness! I would go a step further and complain with Debug.LogError() if it IS indeed null…

Thanks buddy, I was uncertain on the appropriate use of that and I was even going to try removing it/replacing it.

And I did not make a function called "OnCollisionExit2D(), so I think I will do that as well

1 Like

That is always a quick useful thing to try… references can become borked.

ALSO, you can press play and go select the animator in scene while the game is running and click those bools (or ANY parameter) true / false, which will trigger the animation in realtime, so you can test even while things are percolating and running in your game.

It should appeal to a fellow with the wonderful name of GonzoBonDonzo… man, just seeing that name makes me want to go flip all my animators by hand while my game is running, just to see what mayhem I can cause.

1 Like

Thanks for the guidance Kurt, this code seemed to do the trick

public class AttackDetection : MonoBehaviour
{
    public int damage; // declare an interger called damage, do not assign value yet


    private void OnCollisionEnter2D(Collision2D collision) // call the Collision2D to collision
    {
        Animator animator = collision.gameObject.GetComponent<Animator>(); // .....declare "animator" henchforth as "find that collided gamobject, and get component animator on that gameobject"

        if (collision.collider.gameObject.tag == "Weak Point") // if Collision2D occurs on the collider of gameobject (which has been tagged "weak point").....

        {

            animator.SetBool("TakingDamage", true); // animator state of our above declared "animator" called "TakingDamage", will be triggered to true

            collision.gameObject.GetComponent<MonsterHealth>().TakeDamage(damage); // perform the "Take Damage" function inside the Monster Health script

        }

    }

   

    private void OnCollisionExit2D(Collision2D collision)

    {
        Animator animator = collision.gameObject.GetComponent<Animator>(); // .....declare "animator" henchforth as "find that collided gamobject, and get component animator on that gameobject"

        animator.SetBool("TakingDamage", false); // otherwise the animator state of "Taking Damage" should be set to false

    }



}
1 Like