Unity freezes (presumably because there is an infinite loop) when I run this code.
My logic is:
I call updateTurn, which organizes the players by speed and calls actionPhase on the next fastest unit. actionPhase sets the turnState to 1, which turns on the actionPhase GUI. At this point the player clicks the move button, which sets the turnState to 2. While turnState is 2, update polls the users mouseclicks until a valid move is made, at which point turnState is updated to 9, ending the turn.
All methods work properly on their own EXCEPT actionPhase, which always freezes the game. How can I stop the infinite loop from happening?
Sorry about the formatting. I swear it’s impossible to copy/paste code
//Update turn orders players by speed and then calls action phase on them
void updateTurn()
{
int playerSize = players.Count;
int index = 0;
PlayerStats ps;
//turnState = 0;
print("Turn Started");
//calculate fastest player
sortPlayersBySpeed();
index = 0;
//ps = players[index].GetComponent<PlayerStats>();
index = 0;
while(index < playerSize)
{
print("entering loop");
ps = players[index].GetComponent<PlayerStats>();
if(ps.dead == false)
{
print("Starting Action Phase " + index);
actionPhase(players[index]);
print("Ended Action Phase" + index);
}
index++;
}
}
void actionPhase(GameObject player){
turnState = 1;
while(turnState != 9)
{
switch(turnState)
{
case 0:
//No player selected
break;
case 1:
//player selected, actionphase begun, wait for GUI
break;
case 2:
//move selected, update is getting new click positions
print("move selected");
if(moveValid(player, clickPosition) == true)
{
StartCoroutine(moveObject(player, clickPosition));
turnState = 9;
}
break;
case 9:
print("turns over");
return;
}
}
}
void OnGUI () {
if(turnState == 1 || turnState == 2 || turnState == 8)
{
if (GUI.Button (new Rect (10,10, 100, 50), moveButton))
{
print ("you clicked move");
if(turnState == 1)
{
turnState = 2;
}
}
}
}
// Update is called once per frame
void Update () {
if(turnState == 2 || turnState == 8)
{
clickPosition = getTaggedClickPos("Ground");
print(clickPosition);
}
}