Optimization problem

Player gameObjects automatically check if enemies are in range and attack them.
To reduce calculations, gameObjects find the closest enemy every 5 seconds. If the closest enemy is in range, Attack() is activated. If not, then they find the closest enemy after another 5 seconds. Somehow, results of the search alternate between the actual closest enemy and the next closest one. So, if the closest enemy is in range, Attack() activates for 5 seconds and then deactivates for the next 5 if the next closest enemy is out of range. What am I doing wrong?

  void Update()
    {
                    if (inRange == true)
                        Attack();
                    attackTimer -= Time.deltaTime;
              if (attackTimer <= 0)
                {
                    currentTarget = findClosestTarget();
                    if (Vector3.Distance(this.transform.position, target[currentTarget].transform.position) < (maximumRange + 5))
                    {
                        inRange = true;          
                    }
                    else
                    {
                        inRange = false;
                    } 
                        attackTimer = 5;
                }
            }
        }
    }
 int findClosestTarget()
    {
        if (target.Count == 0) return -1;
        for (int i = 1; i < target.Count; i++)
        {
            if (target *== null)*

{
target.Clear();
PopulateTargetList();
}
}
int closest = 0;
float lastDist = Vector3.Distance(this.transform.position, target[0].transform.position);
for (int i = 1; i < target.Count; i++)
{
float thisDist = Vector3.Distance(this.transform.position, target*.transform.position);*
if (lastDist > thisDist && i != currentTarget)
{
closest = i;
lastDist = thisDist;
}
}
return closest;
}

public void PopulateTargetList()
{
GameObject[] enemyUnitsPresent = GameObject.FindGameObjectsWithTag(targetTag);
for (int i = 0; i < enemyUnitsPresent.Length; i++)
{
if (enemyUnitsPresent*.GetComponent().aircraft == false)*
target.Add(enemyUnitsPresent*);*
}
}

The problem is this line:

 if (lastDist > thisDist && i != currentTarget)

The statement ignores this item if it is already the current target, even if it still is the closest one. I believe it will work if you remove && i != currentTarget

There is more issues than this one bug mentioned by @GanemVisk. One of them is use of GameObject.FindGameObjectsWithTag(targetTag); - this is most often than not an expensive method call and can be easily avoided with simple pattern like this one:

using System.Collections.Generic;
using UnityEngine;
public class PlayerTarget : MonoBehaviour
{
	[SerializeField] bool _aircraft = false;
	public static List<PlayerTarget> Instances { get; private set; } = new List<PlayerTarget>();
	public static List<PlayerTarget> Aircrafts { get; private set; } = new List<PlayerTarget>();
	void OnEnable ()
	{
		Instances.Add( this );
		if( _aircraft ) Aircrafts.Add( this );
	}
	void OnDisable ()
	{
		Instances.Remove( this );
		if( _aircraft ) Aircrafts.Remove( this );
	}
}

enabling you to remove local target list entirely and switch to iterating over PlayerTarget.Aircrafts list directly instead and simplifying your code to:

PlayerTarget currentTarget;
void Update ()
{
	if( inRange ) Attack();
	attackTimer -= Time.deltaTime;
	if( attackTimer<=0 )
	{
		currentTarget = FindClosestTarget( out float distSquared );
		float maximumRangeSquared = maximumRange*maximumRange + 5f;
		inRange = distSquared<maximumRangeSquared;
		attackTimer = 5;
	}
}
PlayerTarget FindClosestTarget ( out float distSquared )
{
	PlayerTarget closest = null;
	Vector3 myPosition = transform.position;
	distSquared = float.PositiveInfinity;
	int numAircrafts = PlayerTarget.Aircrafts.Count;

	for( int i=0 ; i<numAircrafts ; i++ )
	{
		var nextAircraft = PlayerTarget.Aircrafts*;*
  •  float nextDistSquared = Vector3.SqrMagnitude( nextAircraft.transform.position - myPosition );*
    
  •  if( nextDistSquared<distSquared )*
    
  •  {*
    
  •  	closest = nextAircraft;*
    
  •  	distSquared = nextDistSquared;*
    
  •  }*
    
  • }*

  • return closest;*
    }
    But this resolves only one side thing. Your main bottleneck remains to be searching through all enemies and can be resolved better by implementing K-D Tree or Spatial Hashing algorithms (bit more advanced stuff)