Hello,
I have an enemy manager script that goes on all my enemies.
int Health = 0;
/// <summary>
/// Apply damage to the enemy.
/// </summary>
/// <param name="Damage">The amount of damag to apply.</param>
/// <param name="DamageType">The type of damage to apply.</param>
public void TakeDamage(int Damage, DamageTypes DamageType = DamageTypes.Slashing)
{
if(stats != null)
{
switch (DamageType)
{
case DamageTypes.None:
break;
case DamageTypes.Bludgenoning:
Health = Mathf.Clamp(Health - (Damage * stats.BludgeoningMultipler), 0, stats.MaxHealth);
break;
case DamageTypes.Piercing:
Health = Mathf.Clamp(Health - (Damage * stats.PiercingMultipler), 0, stats.MaxHealth);
break;
case DamageTypes.Slashing:
Health = Mathf.Clamp(Health - (Damage * stats.SlashingMultipler), 0, stats.MaxHealth);
break;
case DamageTypes.Fire:
Health = Mathf.Clamp(Health - (Damage * stats.FireMultipler), 0, stats.MaxHealth);
break;
case DamageTypes.Acid:
Health = Mathf.Clamp(Health - (Damage * stats.AcidMultipler), 0, stats.MaxHealth);
break;
case DamageTypes.Ice:
Health = Mathf.Clamp(Health - (Damage * stats.IceMultipler), 0, stats.MaxHealth);
break;
default:
break;
}
}
//After taking damage check the health
CheckHealth();
}
/// <summary>
/// Check to see if the enemy's health has been reduced to 0. If so remove the enemy.
/// </summary>
void CheckHealth()
{
if(Health <= 0)
{
Die();
}
}
/// <summary>
/// Destroy the character.
/// </summary>
void Die()
{
Destroy(gameObject);
}
I also have a weapon with a box collider as a trigger that does this:
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Enemy"))
{
EnemyManager enemyManager;
if(collision.gameObject.TryGetComponent<EnemyManager>(out enemyManager) && weaponData != null)
{
enemyManager.TakeDamage(weaponData.Damage, weaponData.DamageType);
}
}
}
but it affects all the enemies. They all die when one dies. What is making this happen? I appreciate the help.