I’m creating different tower types for my game and I’ve come to the “remote AE tower.” This tower is supposed to select a target, fire at that target and do damage to all enemies within an area of it. In my case, my turrets don’t miss, so this isn’t physics-based.
My code is grabbing an array of enemies but it is not sending them damage. Only the current target takes damage. You’ll see my Debugs. I’ve verified that the array contains multiple targets and I’ve asked it to send me the distance calculations. Strangely, the distance calculations show up only once in code for each Shoot() call and the distance is zero, so I’m only getting a distance calc for my current target. I think I just need another set of eyes. Here’s my code.
//curtar = current target
function Shoot(){
var targets = gameObject.FindGameObjectsWithTag("TARGET");
for (var enemy : GameObject in targets){
var distance = Vector3.Distance(curtar.transform.position, enemy.transform.position);
Debug.Log(distance);
if(distance <= explRad){
Debug.Log("Hit " + targets[0] + targets[1] + targets[2]);
enemy.SendMessage( "TakeDamage", damage, SendMessageOptions.DontRequireReceiver );
if(Time.time > nextEffect){
nextEffect = Time.time + rateOfFire/2;
var boom = Instantiate(explPrefab, transform.position, Quaternion.identity);
}
}
}
}
Thanks in advance. 
I think it needs to be:
var targets = GameObject.FindGameObjectsWithTag("TARGET");
(capital G)
But I’m not sure that’ll fix your issue. At first glance I don’t see anything else wrong with it?
You are right. I’ve corrected it in my code but the turret behaves the same. I’m wondering how my array was being populated now.
In any case, I still receive three targets in my array (which is all I have on the field for testing) and a single distance measurement per Shoot() call that is zero. Ideas?
Could it have something to do with them all being instances of a single prefab? Can’t imagine how that would matter.
Do the objects tagged target have colliders? I would suggest using Physics.OverlapSphere. a) It would probably work off the bat b) your current method is pretty cpu intensive, comparatively.
Thanks Legend, appreciate the help.
I’ve tried your suggestion about using Physics.OverlapSphere and continue to experience the same issue. I’ve increased the explosion radius to 100 in order to test and only target takes damage even though I’ve verified that the array contains multiple targets. Here is my current code:
function Shoot(){
var targets = Physics.OverlapSphere(curtar.transform.position, explRad);
Debug.Log("Hit" + targets[0] + targets[2]);
for (var enemy : Collider in targets){
if (enemy.tag == "TARGET"){
enemy.SendMessage("TakeDamage", damage, SendMessageOptions.DontRequireReceiver);
if(Time.time > nextEffect){
var boom = Instantiate(explPrefab, transform.position, Quaternion.identity);
}
}
}
}
It seems odd that I’m using SendMessage on a Collider but it does successfully damage the current target so it is passing the tag check and receiving the message. I’ve put Debuggers all over this thing with no success. It only sends a single damage message out. Ideas?
What does the code look like that calls Shoot()?
This is the code I’m using to call the Shoot function.
function FindClosestEnemy(){
var targets = GameObject.FindGameObjectsWithTag("TARGET");
if (targets.length>0){
for (var enemy : GameObject in targets){
var distance = Vector3.Distance(enemy.transform.position, transform.position);
if (distance < closestDist - 0.2){
LookAtTarget = enemy.transform;
closestDist = distance;
curtar = enemy;
targets = null;
}
if(LookAtTarget){
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if(Time.time > nextFire){
nextFire = Time.time + rateOfFire;
Shoot();
}
}
}
}
}
Since the shoot function is being called and the curtar variable is an enemy, I would assume that this wouldn’t matter. The variable names shared between them are all created in the function itself but it is possible I’ve overlooked something. Thanks again for taking a look. 
Ew, having it all nested like that looks pretty messy!
Anyway, I’m guessing its because you’re setting targets to null when you find a target, and you’re only entering the whole loop that calls Shoot if targets.length>0, so once you find a target, that iteration is going to be the last.
I know it looks ugly. I’m extremely new to thinking in 3D.
The reason I set target to null is so that the turret won’t continue firing once the target is out of range, which only happens a target leaves range and no other is within range. I expect that better methods exist, but this is my first project and I am working solo (except for art assets). I’m open to alternate methods of course but not knowing why this method fails is going to bother me. 
Did you try taking that line out, though? Does the overlapsphere/takedamage message work then?
I’ve commented the line out and the issue persists. The line Debug.Log(“Hit” + targets[0] + targets[2]); shows in my Console. I’ve added lines to tell me when it runs the SendMessage command. It only sends once per Shoot() function. The array is populated with more than one entity according to my debugs asking for target[0], target[1] and so forth. It just seems that it isn’t cycling through the list.