private void Update() { List <GameObject> gameObjects = m_npcTracker.GetList(m_getAllyList); Transform closestObject = startingClosestObject; bool foundFirstClosest = false; for (int i = 0; i < gameObjects.Count; i++) { if (gameObjects[i].activeSelf) { if (!foundFirstClosest) { foundFirstClosest = true; closestObject = gameObjects[i].transform; } else { if (Vector3.Distance(gameObjects[i].transform.position, m_objectTrasform.position) < Vector3.Distance(closestObject.transform.position, m_objectTrasform.position)) { closestObject = gameObjects[i].transform; } } } } if (closestObject != startingClosestObject) { m_attackAI.ChangeTarget(closestObject); } }