Trigger is working for one object but not for the other

Hi there, I created a script for a tower defence, the virus can damage the firewall but the firewall can’t damage the virus. I used some consol log message to check and seems that collider or trigger is not working there. The code is identical from virus and firewall, I just changed the tags and the names of some functions.

here’s a video of the behaviour https://www.loom.com/share/cfddfd04974b48249af66b23e14f102e

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

public class AttackEnemies : MonoBehaviour
{
  public int attackDamage = 10;             // The amount of health taken away per attack.

     GameObject enemy;                          // Reference to the player GameObject.
     EnemyHealth enemyHealth;                  // Reference to the player's health.

     void Awake ()
     {
         // Setting up the references.
         enemy = GameObject.FindGameObjectWithTag ("Enemy");
         enemyHealth = enemy.GetComponent <EnemyHealth> ();
     }


     void OnTriggerEnter2D(Collider2D Enemy)
         {
             Debug.Log("TRIGGER ATTIVO ");
             FightEnemy();
         }


      void OnCollisionEnter2D(Collider2D Enemy)
             {
                 Debug.Log("COLLISION ATTIVO ");
                 FightEnemy();
             }

    void FightEnemy()
    {
      if(enemyHealth.currentHealth > 0)
      {
            // ... damage the player.
            enemyHealth.TakeDamage (attackDamage);
            Debug.Log("FIREWALL attackDamage = " + attackDamage);
      }
    }

}
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{

  public int startingHealth = 1;                            // The amount of health the player starts the game with.
  public int currentHealth;
  bool isDead = false;                                                // Whether the player is dead.

  void Awake ()
{
    // Set the initial health of the player.
    currentHealth = startingHealth;
}

    public void TakeDamage (int amount)
    {

        // Reduce the current health by the damage amount.
        currentHealth -= amount;

        // If the player has lost all it's health and the death flag hasn't been set yet...
        if(currentHealth <= 0 && !isDead)
        {
            // ... it should die.
            Death ();
        }
    }


    void Death ()
    {
        // Set the death flag so this function won't be called again.
        isDead = true;
        Destroy(gameObject);
        Debug.Log("VIRUS IS DEAD");
    }
}

Check the example for OnCollisionEnter2D:

Note that it uses a Collision2D parameter, not a Collider2D parameter. Since you have the wrong parameter type, it will not be called by Unity.

You should try to fix that NullReferenceException(s) and ArgumentOutOfRangeException(s) in the NodeStats script. They could be the problem. Did you forgot to add one of the scripts to the object maybe ?

1 Like

I tried but it’s not working anyways