Switching from GameObject to GameObject by using a list

Hello,

I’ve tried around with Arrays and Lists for a while and got one working. I finally managed to make a list which orders the values of every single GameObject from high to low value. The problem now is that when I click on next turn. He doesn’t add +1 on the list number. The log keeps on updating with 0, 1, 2, 3, 0, 1, 2… etc. It does switch the player but to the number 2 in the list (so actually gameobject 3). How do I fix this? Here is the code:

public class TurnBasedStates : MonoBehaviour {

private CharacterStats statsmanager;
public List<GameObject> characterList = new List<GameObject>();
private GameObject currentCharacter;

public enum BattleStates{
	START,
	ACTIONPHASE,
	ANIMATION,
	ENDTURN,
	LOSE,
	WIN,
	NEXT
}

private BattleStates currentState;

// Use this for initialization
void Start () {
	GameObject[] allCharacters = GameObject.FindGameObjectsWithTag ("Character");
	allCharacters = GameObject.FindGameObjectsWithTag("Character").OrderBy(go => go.GetComponent<CharacterStats>().initiative).ToArray();
	System.Array.Reverse (allCharacters);
		foreach (GameObject character in allCharacters) 
	{
		characterList.Add (character);
	}
 }


// Update is called once per frame
void Update () {
			//Debug.Log (currentState);
			switch (currentState) {
			case(BattleStates.START):
		currentCharacter = characterList[0];
		currentCharacter.GetComponent<CharacterStats>().isActive = true;
			break;
			case(BattleStates.ACTIONPHASE):
					break;
			case(BattleStates.ANIMATION):
					break;
			case(BattleStates.ENDTURN):
					break;
			case(BattleStates.LOSE):
					break;
			case(BattleStates.WIN):
					break;
			case(BattleStates.NEXT):
			currentCharacter.GetComponent<CharacterStats>().isActive = false;

			for (int i = 0; i < characterList.Count; ++i)
			{
				currentCharacter = characterList*;*
  •   			currentCharacter.GetComponent<CharacterStats>().isActive = true;*
    
  •   			Debug.Log(i);*
    
  •   			currentState = BattleStates.ACTIONPHASE;*
    
  •   		}*
    
  •   				//statsmanager = GameObject.Find("Character").GetComponent<CharacterStats>().initiative;*
    
  •   				//currentState = BattleStates.ACTIONPHASE;*
    
  •   		break;*
    
  •   		}*
    
  •   }*
    
  • void OnGUI(){*

  •   	if(GUILayout.Button("END TURN")){*
    
  •   			currentState = BattleStates.NEXT;*
    
  •   		}*
    
  •   }*
    

}

I think you’re tripping yourself up using loops in the Update function.

For example, when you enter the “NEXT” case of the switch, the first thing you do is set currentCharacter to not active… But then loop through every single character turning every single one back on. You print each number to the console, and set the currentState to “ACTIVE” repeatedly. You have changed the state, but it has not left the loop inside the Update yet, so it will not trigger a new case until the next frame after the loop has gone through every single one.

Instead of looping, just increment a pointer (once), check for wrap around, and leave the case.