- Home /
My game freezes
I made a game consisting of 2 levels, when i click play the game runs well, till the player wins. If i press back to go to main menu, and try to open any level again i find it freezing, and don't respond only if i clicked on restart. Any help!
Answer by BluePyramid · Jul 22, 2019 at 07:52 AM
a while statement?
This is a piece of code:
void Start ()
{
gameOver = false;
//restart = false;
//restartText.text = "";
gameOverText.text = "";
Win.text = "";
score = 0;
int i = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Vector3 spawnPosition = new Vector3 (Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y,spawnValues.z );
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
if (score > 100) {
Win.text= "YOU Win";
Time.timeScale = 0;
break;
}
}
//yield return new WaitForSeconds (waveWait);
if (gameOver)
{
//restartText.text = "Press 'R' for Restart";
//restart = true;
Time.timeScale = 0;
break;
}
}
}
Answer by game4444 · Jul 22, 2019 at 08:02 AM
Please post your code what are you doing when player wins. Without checking your code no one could properly tells what and were you are wrong. One things is are you changing Time.timeScale value to 0? Sometimes on win condition we normally change time scale to 0 top stop the game. But don't reset to one. Its my blind guess.
Here is the code: void Start () { gameOver = false; //restart = false; //restartText.text = ""; gameOverText.text = ""; Win.text = ""; score = 0; int i = 0; UpdateScore (); StartCoroutine (SpawnWaves ()); }
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Vector3 spawnPosition = new Vector3 (Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y,spawnValues.z );
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
if (score > 100) {
Win.text= "YOU Win";
Time.timeScale = 0;
break;
}
}
//yield return new WaitForSeconds (waveWait);
if (gameOver)
{
//restartText.text = "Press 'R' for Restart";
//restart = true;
Time.timeScale = 0;
break;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612205256im_/https://answers.unity.com/themes/thub/images/avi.jpg)