List of GameObjects not updating between clears?

I have a set list of gameobjects called turnOrder that will control which gameobject is able to act at a given moment. (Turn based combat base). When this runs the first time, it does all it needs to do in terms of building the list/sorting/ all that jazz, it runs and works just fine. If I toggle combat off, it should clear all lists involved so that the next time combat is engaged, a new list of enemies is formed and it sorts them based on participant speed. But this -does not happen-. If combat is toggled off and then on again, the turnOrder list retains its order from its previous run. It never reupdates the list. I’ve tried turnOrder.Clear(), iterating through the list and removing each item, as well as just making it a new list like it currently does. Nothing works. This has driven me crazy for several days now so any help would be appreciated. Full script below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using EWorld;
public class CombatManager : MonoBehaviour
{
public GameObject turnOrderPanel;
public GameObject turnOrderView;
public StateManager stateManager;
public List<GameObject> enemies;
public List<GameObject> turnOrder;
public GameObject player;
public GameObject whosTurn;
public bool combatStarted;
void Start(){
    turnOrderPanel.SetActive(false);
    stateManager = gameObject.GetComponent<StateManager>();
    player = GameObject.Find("Player");
    combatStarted = false;
}
void Update(){
    if(stateManager.state == StateManager.GameState.Combat){    
        turnOrderPanel.SetActive(true);
        if(combatStarted == false){
            FindEnemies();
            StartCoroutine(SetTurnOrder());
            BuildTurnView();
            combatStarted = true;
        }

        if(combatStarted == true){
            whosTurn = turnOrder[0];
            if(whosTurn != player){ 
                var combatAI = whosTurn.GetComponent<EnemyInCombat>();
                combatAI.DoBattle();
                if(whosTurn.GetComponent<EnemyInCombat>().turnOver == true){
                    NextTurn();
                }
            }
        }
    }


    if(stateManager.state == StateManager.GameState.Combat && combatStarted == true){
        StartCoroutine(CleanUp());   
    }

}

public void FindEnemies(){
    enemies.Clear();
    enemies = GameObject.FindGameObjectsWithTag("Enemy").ToList();
}

IEnumerator SetTurnOrder(){
    List<GameObject> tempOrder = new List<GameObject>();
    tempOrder.Add(player);
    foreach(GameObject enemy in enemies){
        tempOrder.Add(enemy);
    }
    turnOrder = new List<GameObject>(tempOrder.OrderByDescending(x=>x.GetComponent<BaseStats>().speed).ToList());
    yield return new WaitForSeconds(.1f);
}

IEnumerator CleanUp(){
    turnOrder = new List<GameObject>();
    enemies.Clear();
    foreach(Transform child in turnOrderView.transform){
        Destroy(child.gameObject);
    }
    turnOrderPanel.SetActive(false);
    whosTurn = null;
    combatStarted = false;
    yield return new WaitForSeconds(.5f);
}

public void BuildTurnView(){
    Font ArialFont = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
    for(var i = 0; i < turnOrder.Count; i++){
        if(turnOrder[i] == player){
            string playerName = player.GetComponent<PlayerStats>().playerName;
            GameObject playerPanel = new GameObject(playerName);
            playerPanel.transform.parent = turnOrderView.transform;
            Text playerText = playerPanel.AddComponent<Text>();
            playerText.font = ArialFont;
            playerText.text = playerName;
        } else {
            string enemyName = turnOrder[i].GetComponent<EnemyStats>().enemyName;
            GameObject enemyPanel = new GameObject(enemyName);
            enemyPanel.transform.parent = turnOrderView.transform;
            Text enemyText = enemyPanel.AddComponent<Text>();
            enemyText.font = ArialFont;
            enemyText.text = enemyName;
        }
    }

}

public void NextTurn(){

    bool changingTurns = false;
    GameObject tempFirst = turnOrder[0];
    if(changingTurns == false){
        for(var i = 0; i < turnOrder.Count; i++){
            if(i == turnOrder.Count-1){
                turnOrder[i] = tempFirst;
                break;
            }
            GameObject tempNext = turnOrder[i + 1];
            turnOrder[i] = tempNext;
        }
        foreach(Transform child in turnOrderView.transform){
            Destroy(child.gameObject);
        }   
        BuildTurnView();
        changingTurns = true;


    }


}


}

To empty a List, all you need to do is call turnOrder.Clear(). I would avoid reinstantiating the List since that might lead to weird behaviour:

turnOrder = new List<GameObject>();

If you tried that and it still doesn’t clear, then the only logical reason I suppose is because the part of the code with the .Clear isn’t being called. Put some Debug.Log(“test”)s to make sure. If it is being called and it still isn’t clearing, then it might be that your code is not working as you expected. Whenever I run into a non-understandable bug I feel it helps to simplfy things down. Make everything explicit. So maybe have a function like:

public void DisableCombat()
    {
        combatStarted = false;
        turnOrder.Clear();
    }

That way, if something goes wrong, you know exactly why and where it goes wrong.

I would also question your use of the IEnumerator; I think that might be your bug. Firstly I think it seems a little overkill (unless you literally have hundreds of objects that you have to deal with very often). But secondly, because IEnumerators are asynchronous, I think it might be writing to your List even after you have cleared it. In other words, the IEnumerator might be going out of sync with the rest of your code, causing weird behaviour. Hope that helps.