How to trigger an animation based on the collision of two other gameObjects


Here is my scenario, I have three boxes. Box 1 has the script and the animation. Is there a way to trigger the animation in Box 1, based on the collision of Box 2 and Box 3. One note: when Box 2 and Box 3 collides, Box 2 is destroyed.

Just for more specifics if needed: I have an enemy (box 1), shooting a projectile (box 2), at the player (box 3). Upon the projectile hitting the player, the projectile is destroyed and then I want the enemy to jump up and down excited that he hit the player.

I just need help with the triggering of the animation in this scenario.

I have tried a bool based on OnCollisionEnter2D and OnTriggerEnter2D with the projectile and the player, but no matter what I try (which has been many different ways), even though the bool turns true on collision, I can get that bool to register true on the enemy. So I’m guessing this is the wrong way to go about doing this. The only other way I can think of this working is my scenario above, in having the animation trigger based on the collision of two objects (neither of which the script or animation is on).

Any help with this would be great and if you need any other information, just let me know.


The simplest way to do this would be something like this

//Put this function in the script attached to your enemy unit
public void TriggerAnimation(){
    //Triggers the animation of the enemy

//When you instantiate a bullet, give the script below a reference to the enemy object
Gameobject newBullet = Instantiate(blah blah blah)
newBullet.GetComponent<BulletCollisionDetection>().sourceEnemy = this;


//Attach this script to your bullet prefab
public class BulletCollisionDetection : Monobehavior{
    //This will hold a reference to the script that is on your enemy (I have assumed the script is called 'EnemyScript')
    public EnemyScript sourceEnemy;

    public void OnCollisionEnter2D(Collision2D col){
        //Somehow check that you are hitting the player
        if(col.tag == "Player"){