SendMessage to all *tag* within range

I have some code for finding the nearest gameObject with the tag “Enemy” - checkEnemy().
I also have a function to SendMessage to the closest enemy.
But I’d like to target all objects with the Enemy tag, withing the distance of 8, instead of just the nearest one. I hope that explains it properly.

function checkEnemy()
{
	Enemy = GameObject.FindGameObjectWithTag("Enemy");
	
	Enemies = GameObject.FindGameObjectsWithTag("Enemy"); 
    
    var closestDist = Mathf.Infinity; 
 
    for (Enemy in Enemies) { 
        var dist = (transform.position - Enemy.transform.position).sqrMagnitude; 
 
        if (dist < closestDist) { 
            closestDist = dist; 
            closest = Enemy;
            } 
    } 
	if (closest)
	{
		distance = Vector3.Distance(closest.transform.position, Player.transform.position);
	}

---------------------------------- ApplyDamage part

if (Weapon03.active == true)
{

if (particle.isPlaying == false)

{
    	particle.transform.position = particleLocation.transform.position;

    	particle.Play();

if (distance < 8)

	{
    	     closest.SendMessage("ApplyDamage", 100);
    }
  }			

}

Did you write this?

Couldnt you just do almost what your doing like this?

pseudo code:
for(enemy in enemyList)
   get distance(player enemy)
   if(distance is < 8)
     enemy.SendMessage("ApplyDamage", 100);

NB: Dont use capital letters for variables (Enemy, Enemies etc…) it looks like a type when you read it and is annoying :slight_smile: