How to use one script to run two animation with OnCollisionEnter2D

i want to make a player collide with two buttons to open a door , i want to use same script to run two animation using OnCollisionEnter2D then make a door animation run when two buttons are pressed, is there any way instead of using two script ??

public class RedButton : MonoBehaviour
{

   
    private Animator anim;
    void Start()
    {
        anim = gameObject.GetComponentInChildren<Animator>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag ("Player"))
        {
            anim.Play("PushButtonOne");
        }
        else if (collision.gameObject.CompareTag("Player"))
        {
            //second button animation btw its the same animation 
        }
    }
    private void OnCollisionExit2D(Collision2D collision )
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            anim.Play("IdelOne");
        }

        else if (collision.gameObject.CompareTag("Player"))
        {
            //second button animation
        }
    }
}

sorry i wrote this in 2d forum i thought its scripting :frowning:

this wont work, since its the same condition on both:

if (collision.gameObject.CompareTag ("Player"))
        {
            anim.Play("PushButtonOne");
        }
        else if (collision.gameObject.CompareTag("Player"))
        {
            //second button animation btw its the same animation
        }

One way could be adding public string variable to that script,
where it gives that animation name (if its different name).
Then you can use same script, and set different name on each from inspector.

But also nothing wrong using 2 scripts, if its much easier and helps you to go forward.

thanks for replay , yes i have different name for each animations and i know its wont work but i want the player who collide with the two buttons with same script and run two different animations depends in witch object player collide with , i can do it using two scripts and every script got its on OnCollisionEnter2D and each one has one animation but i wonder if i can do it in one script :frowning:

i found a solution after 6 hours of searching thanks guys :slight_smile: