Array - SendMessage to two Targets.

The situation is:

I have a melee weapon script - it consists of the Physics Overlap Sphere, in which targets will get Damage, when the Player pushes Fire button.
The problem is: I need it to Send Message just to the first two of the Targets (of more specificly: number of enemies determined by Max Targets variable).

FOR NOW:

It simply sends message to ALL Enemies in range. How should I limit it to just two enemies?

The Code:

#pragma strict

public var MeleeRange : float = 5;
public var TheDamage : float = 500;
var TargetsInRange = new Transform[2]; //<-- Number "2" is there for a placeholder.
var MaxTargets : int = 2; //<-- This should be the number of Enemies to SendMessage.


function Update ()
{

 var TargetsInRange = Physics.OverlapSphere(transform.position, MeleeRange);

	
	for each (var Target in TargetsInRange)
	{
		if (Input.GetButtonDown("Fire") && Target.gameObject.transform.tag == "Enemy")
		{
		Target.SendMessage("ApplyDamageNormal", TheDamage, SendMessageOptions.DontRequireReceiver);
		}
	}
}

Try changing your for() to something like this:

for (var i = 0; i < Mathf.Min(TargetsInRange.Length, MaxTargets); i++)
{
	var Target = TargetsInRange*;*

if (Input.GetButtonDown(“Fire”) && Target.gameObject.transform.tag == “Enemy”)
{
Target.SendMessage(“ApplyDamageNormal”, TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
Note I would suggest moving your ‘Input.GetButtonDown(“Fire”)’ enclosing everything else in this function. There is no reason to do the Physics.OverlapSphere() if the button did not go down this frame.