- Home /
Problem with Coroutines and waitforseconds.
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.,
Please, provide the code you currently have.
The answer may be as simple as calling StopCoroutine
, but without any code, it's impossible to help you accurately.
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!
If you are having trouble with using StopCoroutine then remember you can store a reference to your running coroutine in a variable. Ty to do that ins$$anonymous$$d of starting and stopping them with string argument.
Answer by surfuay · Jan 15, 2019 at 07:52 PM
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.
Yes, i am learning through the udemy tutorial, but this problem is not encountered by him (yet in UI section), I have the same coding as mentioned in the tutorial, but it is a bug which, i guess, went unnoticed by him.
And i am not able to figure out the solution as i do not know exactly how the 'yield return new waitforseconds' statement works, does it pause the whole script or just the block in which the waitforseconds is?
I tried this, (can't attach script)
(inside spawn manager script) public void stopRoutines() { StopCoroutine("Enemy_Spawn") ; StopCoroutine("Powerup_Spawn") ; }
And i called the function from game manager script whenever the variable 'gameover' is true, but it didn't work(no errors and no difference in the way the game runs were noticed) .
So, please if you can tell me, like what is the compiler doing when it encounters a waitforseconds statement.
I could make a successful attempt at this.
BTW Thanks for the reply.
sorry just saw this
post the FULL scripts you're using that reference this. i'll make sure to read through them all
if i remember correctly there should be 2 maybe 4
spawn manager script game manager script $$anonymous$$AYBE the ui script $$anonymous$$AYBE the player script
altho i think the spawn and game script should be fine since we're just trying to work through the spawn aspects not the resetting of scores or reinstantiating the player
Your answer
Follow this Question
Related Questions
Only the first half of my coroutine works 1 Answer
Run Coroutine once, but finish Lerping 2 Answers
How to gradually increase difficulty. 1 Answer
Having trouble using coroutines, making unity hang 0 Answers
coroutine in update method 1 Answer