Collision2D with three gameobjects

Hello,

This is in 2D. I have my player, an enemy, and a projectile from the enemy. The projectile is destroyed when it hits the player. I put a bool on the projectile that when it hits the player, it turns true. I have this coded in Update on the enemy that when the bool on the projectile turns true, and animation goes off for the enemy. In the console, when the projectile hits the player, the bool is true. However, it is not true on enemy script. Any ideas why?

Here is the script on the projectile.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileHitPlayer : MonoBehaviour
{
     
    public bool projectileHit;    
    public GameObject enemy;


    void Start()
    {
        projectileHit = false;       
    }

    void OnTriggerEnter2D(Collider2D colproj)
    {
        if (colproj.gameObject.CompareTag("Player"))
        {            
            projectileHit = true;
            Destroy(gameObject);
        }
    }
}

Here is the script on the Enemy:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyHitPlayer : MonoBehaviour
{
    Animator anim;      
    bool hit;
    public GameObject enemyProj;     
    ProjectileHitPlayer projectileHitPlayer;   


    void Start()
    {
        hit = false;
        anim = GetComponent<Animator>();        
        projectileHitPlayer = enemyProj.GetComponent<ProjectileHitPlayer>();        
    }      

   void Update()
    {      
        if (!hit)
        {
            if (projectileHitPlayer.projectileHit == true)
            {                    
                hit = true;
                anim.SetTrigger("HitPlayer"); 
            }
        }       
    }
}

Anyone help on this is great.

Also one last question: I have three boxes and a script on box 1. I want the script to say that when box 2 and box 3 collide, box 1 does something. Is that possible to do and if so how?

Thanks!
Nic

You aren’t getting the projectileHit bool from that specific ProjectileHitPlayer. You are only getting it from the prefab. When you instantiate the projectile get the hit script from the new GameObject.

public GameObject enemyProj;

    public void Update()
    {
        // if (Enemy close to player shoot)

        // Gets the newly instantiated projectile as a GameObject
        // GameObject projectile = Instantiate(enemyProj, transform.position, transform.rotation);

        // ProjectileHitPlayer php = projectile.GetComponent<ProjectileHitPlayer>();

        //if (php.hit == true)
        //{
            //anim.SetTrigger("HitPlayer");
        //}
    }

Thanks @TomatJuice for the response. I’m still not getting the animation to work, so I’m probably doing something wrong. I put this on my shooting script which instantiates the projectile. it looks like this:

  public void ShootProjectile()
    {        
        if (player != null)
        {
            if (!gm.paused)
            {
                if (shootPointRight1 == null)
                {                    
                    GameObject projectile = Instantiate(projectileLeft1, shootPointLeft1.position, shootPointLeft1.rotation);                    
                    ProjectileHitPlayer php = projectile.GetComponent<ProjectileHitPlayer>();
                    if (php.projectileHit == true)
                    {
                          Debug.Log("1");
                          anim.SetTrigger("HitPlayer");
                     }
                 }
            }
       }
 }

The debug “1” is not showing up in the console. The projectileHit bool is showing true in the console from the ProjectileHitPlayer script which is on the projectile.

Should this be on a different scripts?

Thanks!