In Unity C#, I'm using a coroutine to display a simple pattern on the screen after x seconds using the line "yield return new WaitForSeconds(1.5f)" but after it's first called, it changes isPlayerTurn from false to true.
void Update () {
if (!isPlayerTurn) {
pattern.Add (Random.Range (1, 5));
Debug.Log (isPlayerTurn);
StartCoroutine(ShowPattern());
isPlayerTurn = true;
}
pointGUI.GetComponent<UnityEngine.UI.Text> ().text = "Points: " + playerPoints;
}
private IEnumerator ShowPattern() {
Debug.Log (isPlayerTurn);
yield return new WaitForSeconds (1.5f);
Debug.Log (isPlayerTurn);
// etc
}
The output of the logs are
False
False
True
Is there a reason for this behavior or is it a logic error?
isPlayerTurn, you changeisPlayerTurn. The code that changes it is right there in your question. Can you clarify by explaining what behaviour you were expecting and why? - user743382isPlayerTurnshould still beFalseafter that yield because it's necessary through theShowPattern()function and should beTrueonly afterShowPattern()finishes executing (on the //etc part). - Jean Catanhofalse, I'm asking why you think the current code would keep itfalse. - user743382isPlayerTurn = true;would execute only afterShowPattern()finished. - Jean CatanhoDebug.Logcalls (something likeDebug.Log("Update " + isPlayerTurn)so you know exactly which debug output is which. - Jiri P.