Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

My Coroutine Ignores the Boolean

$
0
0
In another script, a bool controls when this coroutine starts and stops: IEnumerator SpawnWaves () { if (SceneManager.GetActiveScene().buildIndex == 7) { shrinkStage = true; } if (shrinkStage == false) { yield return new WaitForSeconds(startWait); while (true) { for (int i = 0; i < hazardCount; i++, fireCometCount++) { //var asteroidY = Random.Range (Mathf.RoundToInt(-spawnValues.y), Mathf.RoundToInt(spawnValues.y)); //var collectY = Random.Range (Mathf.RoundToInt(-spawnValues.y), Mathf.RoundToInt(spawnValues.y)); var random = Random.Range(0, 3) + 1; //Debug.Log (Mathf.RoundToInt(-spawnValues.y) + " - " + Mathf.RoundToInt(spawnValues.y)); //if (asteroidY == collectY) { if (random == 1) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnWait); } else if (random == 2) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(collectable, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnWait); } if (fireCometCount == 20) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(comet, spawnPosition, spawnRotation); fireCometCount = 0; yield return new WaitForSeconds(spawnWait); } } yield return new WaitForSeconds(waveWait); if (gameOver) { restartText.text = "Restart"; restart = true; //break; } } } } But in another script this bool is completely ignored: IEnumerator SpawnShrinkWaves() { if (gameController.waveTransition == false) { while (true) { for (int i = 0; i < hazardShrinkCount; i++) { var random = Random.Range(0, 12) + 1; if (random == 1 || random == 2 || random == 3 || random == 4) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnTime); } else if (random == 5 || random == 6 || random == 7 || random == 8) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(collectable, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnTime); } else if (random == 9 || random == 10) { Vector3 spawnPosition = new Vector3(spawnValues.x, Random.Range(-spawnValues.y, spawnValues.y), spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate(shrinkHazardOne, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnTime); } else if (random == 11 || random == 12) { Vector3 spawnPosition = new Vector3(11, -0.2f, -2); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazardBarrier, spawnPosition, spawnRotation); yield return new WaitForSeconds(spawnTime); } } } } } I know it's not the bool itself, because this works in the same script: void SpawnBarrier() { if (gameController.waveTransition == false) { Vector3 spawnPosition = new Vector3(11, -0.2f, -2); Quaternion spawnRotation = Quaternion.identity; Instantiate(hazardBarrier, spawnPosition, spawnRotation); } } So why does the Coroutine ignore the bool? Why doesn't the loop only start when waveTransition == false? I want my Coroutine to play, and stop during the wave transition, then play again. By the way, I've never got StopCoroutine to work. I've always stopped coroutines with a boolean, which has worked until now! I can't figure out what's different. If anyone could help me that would be much appreciated.

Viewing all articles
Browse latest Browse all 184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>