Tower defense "First" targeting problem

Ok so im trying to set up my turrets to fire at the “First” guy. so i made this script to shoot the farthest guy in the lane, that is within the range of the turret. im stumped… it looks like it should work to me. but in game the targeting is all around the map, he sometimes shoots the first guy, but mostly his targeting is just unpredictable.

can anyone see whats going wrong?
or if you have a better way of aiming id love to hear it.

( fyi the enemies are given a number as they spawn. first enemy has ID of 0 and the last guy in the wave (#20) has a ID of 19.)

function reAim()
{
	smallestNum = 100; //if it was at 0 he wouldnt aim at anyone
	for(var target: GameObject in Spawner.enemyRanks) //for each guy in the enemy array
	{
		if(Vector3.Distance(Tilt_Aimer.position, target.transform.position) < range) // if this particular guy is in range
		{
			targetNum = target.GetComponent(Evil_Cube).getID(); //get his wave ID
			if(targetNum < smallestNum)//If its smaller than the other IDs this turret has been though
			{
				targetNum = smallestNum; //set the smallest encountered ID to his				
				myTarget = target; //and aim at him
			}
		}
	}
}

Shouldn’t this line:

targetNum = smallestNum; //set the smallest encountered ID to his

be this: (to update the smallest number…)

smallestNum = targetNum; //set the smallest encountered ID to his

~Scorp

oh good god lol… cant believe i didn’t see that

yeah works perfectly now, thanks!

No problem at all, those simple things happen all the time and can drive us insane tracking them down lol Glad to hear it’s working now :slight_smile:

~Scorp