Turret not working correctly

So, before I tried to use lists, I had my turret working correctly. It would aim at targets and deal ‘damage’. However, I discovered that it would instantly switch to the next object which entered into its ‘range’, meaning only a set amount of damage could be dealt to the first one, before moving onto the next one.

A member suggested I used a list to fix this.

I tried.

I broke it, and I need assistance.

import System.Collections.Generic;

var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var muzzleEffect : GameObject;
var myTargets : List.<Transform> = new List.<Transform>();
var muzzlePosition : Transform[];
var turretBall : Transform;
var myTarget : Transform;
var counter : int = 0;

private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;


function Start () {

}

function Update () 
{
	
	if (counter > 0)
	{
	
	myTarget = myTargets[0];
	

	if(myTarget != null)
	{
		if(Time.time >= nextMoveTime)
		{
			CalculateAimPosition(myTarget.position);
			turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
		}
		
		if(Time.time >= nextFireTime)
		{
			FireProjectile();
		}
	}
	
	}
}

function OnTriggerEnter(other : Collider)
{
	if(other.gameObject.tag == "Enemy")
	{
		nextFireTime = Time.time+(reloadTime*.5);
		myTargets.Add(other.transform);
		counter++;
	}
}

function OnTriggerExit(other : Collider)
{
	if(myTargets.Contains(other.transform))
	{
		myTargets.Remove(other.transform);
		counter--;
	}
	
	
}

function CalculateAimPosition(targetPos : Vector3)
{

	desiredRotation = Quaternion.LookRotation(myTarget.position - turretBall.position);
}

function FireProjectile()
{
	nextFireTime = Time.time+reloadTime;
	nextMoveTime = Time.time+firePauseTime;
	
	for(theMuzzlePos in muzzlePosition)
	{
		var newBall = Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
		newBall.GetComponent(CannonBallScript).myTarget = myTargets[0];
		Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
	}
}

Before jumping too much into your code: what exactly is not working? And do you get any errors?

Best way to tackle down such issues is to just print what’s going on to the console via Debug.Log(). Also you may set the inspector to debug mode ( Unity - Manual: Working in the Inspector ) to also see private variables during runtime.

Well, essentially, the turret will locate the first enemy and destroy it, then the turret stops working altogether.

Shameless self-bump.

I’ve identified a more detailed explanation of the problem - the turret fires at the first object and destroys it, then the targeting system fails, as well as the firing system.

EDIT: Apologies for double post.