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");
}
}