- Home /
Issue with nested coroutines getting stuck
So I'm having an issue yielding coRoutines from coRoutines in my game. First just a touch of background. In my game after a player has scored a point both players then return to their "starting" location, then the puck is inbounded and gameplay starts again. I have a few coRoutines all chaining into one another to get this to work.
When you score a goal is sets off a coRoutine in the gameManager that will update your score, that part works great.
public IEnumerator scoreUpdate (string scoringPlayer, int scoreValue) {
points[scoringPlayer] += scoreValue;
if (points[scoringPlayer] >= scoreMax) {
rounds[scoringPlayer]++;
turnRound();
}
yield return StartCoroutine(resetPlayerPos());
Debug.Log("out of Co");
foreach (KeyValuePair<string, GameObject> player in players) {
if (player.Value.name != scoringPlayer) {
puckToPlayer(player.Value.transform);
}
}
}
After the score is updated I call yield to a coRoutine that should run until all players get back to their starting positions. I'm doing that as follows
IEnumerator resetPlayerPos () {
foreach (characterMotor motor in playerMotors.Values) {
StartCoroutine(motor.moveToStart());
}
foreach (KeyValuePair<string, bool> playerPos in atStartPos) {
while (playerPos.Value == false) {
yield return null;
//Debug.Log(playerPos.Key + " " + playerPos.Value.ToString());
}
}
}
Which is in turn contacting the following coroutine on my player and telling him to move back to his start point.
public IEnumerator moveToStart () {
float progress = 0f;
canMove = false;
gameManager.Instance.atStartPos[gameObject.name] = false;
Vector3 initPos = transform.position;
while (progress < 1) {
transform.position = Vector3.Lerp(initPos, playerStartPos, progress);
progress += Time.deltaTime * resetPosSpeed;
yield return null;
}
gameManager.Instance.atStartPos[gameObject.name] = true;
canMove = true;
Debug.Log(gameObject.name + " move Complete!");
}
Ok so now that I have bored almost everyone out of here with pasted code I can get to the exact issue. The character position reset seems to all be working fine. However the following section of the resetPlayer method in my gameManager never seems to exit.
foreach (KeyValuePair<string, bool> playerPos in atStartPos) {
while (playerPos.Value == false) {
yield return null;
//Debug.Log(playerPos.Key + " " + playerPos.Value.ToString());
}
Which is odd because the coRoutine on the actual player itself should change that value here
gameManager.Instance.atStartPos[gameObject.name] = true;
I'm SUPER confused as to why this isn't working... And why that coRoutine gets stick
Answer by darthbator · Mar 13, 2013 at 12:59 AM
Just to sort of close the loop on this one the issues was that I was checking for a value change in the KeyValuePair that I make in the loop and not in the class level Dictionary. I changed it to the code below and now it works awesome!
List<string> playerPosStrings = new List<string>(atStartPos.Keys);
foreach (string entry in playerPosStrings) {
while (atStartPos[entry] == false) {
yield return null;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094238im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Example of co-routine for documentation 1 Answer
While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 1 Answer
Coroutine Won't Stop Using IEnumerator 1 Answer
Bullet Script 2 Answers
WebGL loader stuck at 90% 0 Answers