I have a script that checks for all enemies within a given radius of a “bomb” GameObject in my game, and then applies damage to them using a foreach loop, like this:
Collider2D[] cols = Physics2D.OverlapCircleAll(transform.position, explosionRadius, mask);
foreach(Collider2D col in cols)
{
if(col.gameObject.transform.root.GetComponent<Health>() != null)
{
col.gameObject.transform.root.GetComponent<Health>().TakeDamage(explosionDamage);
}
}
When this bomb explodes on many enemies (40+) my game performance suffers a lot, as per the image below. I was wondering if there is a more efficient way of applying damage to my enemies, since I would like to have many enemies on the screen at once exploding, etc. Thank you!