Help me make a better target finder for my 3d game

Hey so im making a target finder for my game its a 3d game where this tower need to find the nearest target and only focus on that one this code works fine with the enemy coming closer so if he is 2 meter from the tower and moves 1 meter closer it workd but if he moves further away like he move 3 meter away the tower still think he is 1 meter away so can anyone help me make a better target finder?

    public void TargetFinder()
    {
        foreach (var x in enemies)
        {
            float distance = Vector3.Distance(transform.position, x.transform.position);

            if (distance < currentTargetDistance)
            {
                myTarget = x;
                displayText.text = "My target: " + x + "

Distance to Target: " + distance; //skal fjernes nå det er helt klart
currentTargetDistance = distance;
}
}
}

Try just clearing your currentTargetDistance every time you call, so it will always find the closest enemy even if that enemy is farther than the previous closest enemy.
**
Just add currentTargetDistance = Mathf.Infinity right before foreach(var x in enemies). This will keep it from remembering what the last value is, which you don’t seem to need to do.