Hello. I write Space Shooter. The Gamecontroller script has a couroutine responsible for respawning opponents.

IEnumerator SpawnEnemies() { yield return new WaitForSeconds(timeBeforeSpawning); while(true) { if (currentNumberOfEnemies <= 1) { waveNumber++; waveText.text = "Волна: " + waveNumber; float randDirection; float randDistance; for (int i = 0; i < enemiesPerWave; i++) { randDistance = Random.Range (10, 25); randDirection = Random.Range (0, 360); float posX = this.transform.position.x + (Mathf.Cos ((randDirection) * Mathf.Deg2Rad) * randDistance); float posY = this.transform.position.y + (Mathf.Cos ((randDirection) * Mathf.Deg2Rad) * randDistance); Instantiate(enemy, new Vector3(posX, posY, 0), this.transform.rotation); currentNumberOfEnemies++; yield return new WaitForSeconds(timeBetweenEnemies); } } yield return new WaitForSeconds (timeBeforeWaves); } } 

In another script that controls the player, there is a line stopping the execution of the couroutine when the player is running out of health points.

 if (hp == 0) { GameOverText.enabled = true; GameController gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent("GameController") as GameController; gameController.StopAllCoroutines(); } 

In case all the opponents in the wave are killed by the laser (the standard scenario of their death), everything goes well. But if at least one opponent breaks about the player, regardless of the amount of player health, the opponents will spawn. Tell me, pliz, where to dig?

  • The simplest thing is to take a debugger and podobezhit at the moment when the противник разбивается об игрока .... Because there can be a lot of reasons at the moment and not in the provided code ....... maybe somewhere in the variables fall the values ​​and, as a result, the conditions inside the coroutine do not work out ..... maybe somewhere else there is a trigger / event due to which the coroutine is turned off .... or maybe there is a condition somewhere and there is a general Destroy(Gamecontroller) or Disable(Gamecontroller) - Alexey Shimansky
  • Thank. The problem was really a bit apart from the code I attached. I forgot when the enemy died from a collision with a player to unscrew the counter of the number of opponents. As a result, the moment of respawn could not come due to the fact that the corresponding korutina believed that there were more of them than was actually on the screen =) - Nick Barykin
  • AlexeyShimanskyTelepatLevel += 1; : D - Alexey Shimansky

0