Problem with colliders need help

I am making a tower defence game and i am using a sphere collider to target enemys moving throught the collider, but my problem is that if a enemy is destroid inside the collider(that is by deleting it from the scene if it matter) it will not target the next with the tag enemy.
Here is my code

using UnityEngine;
using System.Collections;

public class Ground_Turret : MonoBehaviour {

public GameObject myProjectile;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;

public float errorAmount = .001f;
public Transform myTarget;
public Transform[] muzzlePositions;
public Transform turretBall;

private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;

void  Start (){

}

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

void  OnTriggerEnter ( Collider other  ){
	if(other.gameObject.tag == "Ground Enemy")
	{
		nextFireTime = Time.time+(reloadTime*.5f);
		myTarget = other.gameObject.transform;
	}
}

void  OnTriggerExit ( Collider other  ){
	if(other.gameObject.transform == myTarget)
	{
		myTarget = null;
	}
}

void  CalculateAimPosition ( Vector3 targetPos  ){	
	Vector3 aimPoint= new Vector3(targetPos.x+aimError, targetPos.y+aimError+1700//need to add 1700 or it is gonna aim stright down the ground
			, targetPos.z+aimError);
	desiredRotation = Quaternion.LookRotation(aimPoint);
}

void  CalculateAimError (){
	aimError = Random.Range(-errorAmount, errorAmount);	
}

void  FireProjectile (){
	nextFireTime = Time.time+reloadTime;
	nextMoveTime = Time.time+firePauseTime;	
	CalculateAimError();
	
	foreach(Transform theMuzzlePos in muzzlePositions)
        {
            Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
        }
}
}

I would use a List of enemies entered (add when enters and remove when exits), so you will set myTarget to the nearest transform from the list when it is null (you set it null as you already do). May be your code doesn’t work because the other enemies entered the trigger before the target was destroyed, using a list you will fix it.

I am pretty new to programming how would you do that.

It’s something like

List enemiesTarget = new List();

void OnTriggerEnter ( Collider other ){

if(other.gameObject.tag == “Ground Enemy”)

{
enemiesTarget.Add(other.gameObject.transform);

nextFireTime = Time.time+(reloadTime*.5f);

myTarget = enemiesTarget[0];

}

}

void OnTriggerExit ( Collider other ){

if(other.gameObject.transform == myTarget)

{

enemiesTarget.Remove(other.gameObject.transform);

}

}

Sorry, i don’t know how to put in line code here.

Would this mean i need to add all enemys to a list or does it add them automaticly because if they dont it kinda destroys the thing with enemy waves in tower defence game, only asking because i am new.

You’ll add automaticlly when enters on trigger, and just destroy the element of the list when the enemy is destroyed or remove from the list when goes out of the trigger.

Ah thanks I am gonna try it out.

I am getting this error

so do i need to add a varibel where i call something List 1? and what should be infront of it?

You need to do a reference to use a List:

using System.Collections.Generic;

Ok it wont remove targets from its list and if I destroy a target now while having two targets within the sphere collider it dont target number two but it remember target one and can see target two. I just want to say thank you so much for this help you are so awesome.

Yeah, you’re welcome :slight_smile:

Glad that works!

its not comepletly solved as i typed above it wont detect when i move targets in and out of the sphere collider and when i destroy a target it will not search for a new one plus it keeps shooting it never stop shooting even if i move it outside the collider it still can see the enemy, and just to make a recap my code looks like this now:

using UnityEngine;
using System.Collections;
using System.Collections.Generic; 
public class Ground_Turret : MonoBehaviour {

public GameObject myProjectile;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;

public float errorAmount = .001f;
public Transform myTarget;
public Transform[] muzzlePositions;
public Transform turretBall;

private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;
	
public List<Transform> enemiesTarget = new List<Transform>();
void  Start (){

}

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

void  OnTriggerEnter ( Collider other  ){
	if(other.gameObject.tag == "Ground Enemy")
	{
		enemiesTarget.Add(other.gameObject.transform);	
		nextFireTime = Time.time+(reloadTime*.5f);
		myTarget = enemiesTarget[0];
	}
}

void  OnTriggerExit ( Collider other  ){
	if(other.gameObject.transform == myTarget)
	{
		enemiesTarget.Remove(other.gameObject.transform); 
	}
}

void  CalculateAimPosition ( Vector3 targetPos  ){	
	Vector3 aimPoint=new Vector3(targetPos.x+aimError, targetPos.y+aimError+1700//need to add 1700 or it is gonna aim stright down the ground
			, targetPos.z+aimError);
	desiredRotation = Quaternion.LookRotation(aimPoint);
}

void  CalculateAimError (){
	aimError = Random.Range(-errorAmount, errorAmount);	
}

void  FireProjectile (){
	nextFireTime = Time.time+reloadTime;
	nextMoveTime = Time.time+firePauseTime;	
	CalculateAimError();
	
	foreach(Transform theMuzzlePos in muzzlePositions)
        {
            Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
        }
	}
}