I am stuck in galaxy shooter sort of game wherein i need to spawn enemies every 3 seconds, and it is working fine, but when the player dies, and if the game is restarted before 3 seconds, there are 2 coroutines running at the same time, thus spawning 2 enemies every 3 seconds. I have used Instantiate and yield return new waitforseconds(for 3 seconds) in the ienumerator of the coroutine.Please help.,
not too sure what the problem is (i’m betting you’re doing a tutorial on udemy ;)…(which i loved, if you are)
post your code for how you restart the game but I did it this way
public void StartOver ()
{
//load the play scene
//call the uimanager new game
// set the gamemanager game over to false
// set all game over objects to false again
SceneManager.LoadScene("Play_Scene");
UIManager.uIManager.NewGame();
GameManager.gameManager.gameOver = false;
Destroy(Player_Dead.dead);
//panel communication
UIManager.uIManager.pauseMenuPanel.SetActive(false);
UIManager.uIManager.pauseMenuVisible = false;
UIManager.uIManager.gameOverPanel.SetActive(false);
UIManager.uIManager.gameOverPanelVisible = false;
}
a bunch in there you don’t need prolly, the main part is
SceneManager.LoadScene(“whatever your scene is named”)
it will do a complete wipe of everything as long as you tell it to set everything back to defaults (gameover = false, my NewGame() is what sets scores back to 0 and lives back to x) but this will at least stop your current coroutine and start the new one so that you don’t have a problem with overlapping coroutines) and you won’t notice that the scene reloaded.
Good day.
Is a good general practice to control the corutines with StartCorutine and StopCorutine all time you need it.
If the player dies, Stop all corutines you think can go wrong.
Then StartCorutine again when the player respawns again.
This way you will have better control of what corutines are executing all time.
Bye!