targeting multiple enemies issue

For some reason I’m getting an error with this code. What am I doing wrong? I want it to engage at any target with the same tag but I keep getting the same error on the var targetDir “Not set to an instance” I believe is what it says.

var target : GameObject[];
var Range = 4;

function Update()
{
	target = GameObject.FindFindGameObjectsWithTag("Enemy");
	var forward = transform.forward;
	var targetDir = target.position - transform.position;
	var angle = Vector3.Angle(targetDir, forward);
	if(Vector3.Distance(transform.position, target.position) < Range)
	{
		if(angle < 45)
		{
			Engage();
		}

target is an array of gameObjects- it doesn't have a 'position' member variable! I think for this to make sense, you need to loop over all the members of 'target'. Do you want to call the 'Engage()' method for every single enemy in range, or only for one? In any case, the code is not much different.

for(var curTarg : GameObject in target)
{
    var targetDir : Vector3 = curTarg.transform.position - transform.position;
    if(Vector3.Distance(curTarg.transform.position, transform.position) < range && 
        Vector3.Angle(targetDir, transform.forward) < 45)
    {
        Engage();

        // if you want to call Engage for every singe candidate
        // (rather than if there is only one), comment this out.
        break;
    }

}

Well you see target is an array, just saying target.position isn’t going to have a value. But you do target[0].position will give you the position of the first gameobject in that array. You could check each of them by doing something like this:

var objectArray : GameObject[];
var Range = 4;

    function Update()
    {
        objectArray = GameObject.FindFindGameObjectsWithTag("Enemy");
        for (int i = 0; i < objectArray.Length; i++)
        {
            var target = objectArray*;*

var targetDir = target.position - transform.position;
var angle = Vector3.Angle(targetDir, forward);
if (Vector3.Distance(transform.position, target.position) < Range)
{
if (angle < 45)
{
Engage(target);
}
}
}
}