I am having a problem with a foreach loop. When my AOE ability hits an enemy it creates a CircleCollider2D and puts all the enemies on a list, which then the loop goes through and adds damage to their scripts.
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
targets.Add(other);
foreach (Collider2D enemy in targets)
{
EnemyHealth hp = enemy.GetComponentInChildren<EnemyHealth>();
hp.TakeDamage(abilityConfig.baseDamage);
print(enemy.name + " takes " + abilityConfig.baseDamage + "damage!");
}
}
}
But as you can see from the picture below the loop goes 3 times over one enemy, 2 times over the second one and once over the third.
As these enemies are the same type they all have the same EnemyHealth script. If anyone would point me in the right direction I would greatly appreciate it.
targets
- itself. \$\endgroup\$ – Polar May 8 '19 at 11:21