Enemy AI help how to restart collision detection?

Hi, in my tower defense game, I have soldier units from barracks and theyare catching enemies.

My soldierAttack.cs applied on soldier units. It works very well but only for 1 enemy. After they killed they stop searching enemies. I couldnt figure it out how to restart void on trigger after they killed enemy. I need to restart void on trigger function, after this state is true; if(other.gameObject.getcomponent().health <=0) but i dont know where do i need to put?

edit: “Ground” tag is “enemy” tag.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SoldierAttack : MonoBehaviour {
	
	private NavMeshAgent agent;
	private bool contacted = false;
	private NavMeshMove enemymove;
	public float askerDamage;
	public float timer = 0;
	public float damageTime = 10;
	private bool stayingContact = false;
	private Collider enemy;
	
	void Start() {
		
		agent = GetComponent<NavMeshAgent>();
		
	}
	
	void OnTriggerEnter (Collider other){
		
		Debug.Log ("contacted");
		contacted = true;
		
		
		if (stayingContact)
		{
			return;
		}
		
		if(other.gameObject.tag == "Ground" ){
			
			stayingContact = true;
			enemy = other;
			agent.SetDestination(other.gameObject.transform.position);
			enemymove = other.gameObject.GetComponent<NavMeshMove>();
			enemymove.enabled = false;
			other.gameObject.GetComponent<NavMeshAgent>().enabled = false;
			
		}
		
		
	}
	
	void OnTriggerStay(Collider other){
		
		if (enemy == null || enemy != other)

			 return;
		
		if (other.gameObject.tag == "Ground") {
			
			if (timer >= damageTime)
			{
				timer -= damageTime;
				other.gameObject.GetComponent<Properties>().Hit(askerDamage);

				}
			}
			
			timer += Time.deltaTime;
			
		}
		
	}

i wouldnt even bother using collisions for this, just a distance to enemy check should be sufficient.

make a statemachine for the soldiers,

isAttacking

isMoving

hasTarget

then check like

if (!hasTarget)
{
findTarget(); //here you could pick a target from list of available enemies

}

void findTarget()
{
//search through available enemies and pick one. if successful then set hasTarget to true and isMoving to true
}


if(isMoving)
{
if(distanceToTarget >= attackDistance)
{
moveToTarget(); // here you can select the enemies position that you chose and use pathfinding to get there
}
else //soldier is within attack range
{
isMoving = false;
isAttacking = true;
}

}  

if(isAttacking)
{
if(enemyHealth > 0)
{
damageEnemy(); // or whatever your routine is
}
if(enemyHealth <=0)
{
hasTarget = false;
isAttacking = false;
isMoving = false;


}
//at this point you may want to see if the soldier returns to base or just continues to attack enemies
}

its all kinda pseudocode but i hope you can get the general idea from this.

If it is supposed to damage multiple at once use this:

float ViewDistance;

void Update() {
    foreach (Collider collider in Physics.OverlapSphere(transform.position, ViewDistance))
    {
       if (collider.tag == "Ground") {
            //ATTACK
       }
    }
}

If not use this:

float ViewDistance;
    
void Update() {
  Collider col = Physics.OverlapSphere(transform.position, ViewDistance)[0];
   {
        if (col.tag == "Ground") {
           //ATTACK
        }
   }
}

Ok. I found the answer by another friend;

just I add one line in script like this;

if(other.gameObject.Getcomponenet<enemyscript>(). health <=0)
{
enemy= null;
stayingContact = false;
}

thank you again for working my mind.