Find closest enemy and target it

I have code:

private Transform target;

	GameObject FindClosestEnemy() {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("tag1");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos) {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance) {
                closest = go;
                distance = curDistance;
            							}
									}
		return closest;
		
    								    }
	void Update() {
	    
		if (target != null){
			transform.LookAt(target);
		    transform.position = Vector3.MoveTowards (transform.position, target.position, 100.0f * Time.deltaTime);
							}
		
					}
}

But i’m new to unity and i wanted to know how i can add this ClosestEnemy to my target.

Run the script every x frames. You can do that in Update every frame, but that might be too heavy with a lot of enemy. I suggest you do that inside a coroutine with a frequency.

Another way is to use a sphere collider as a trigger, to make sure there is targets nearby. Or Physics.OverlapSphere.