Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sharmajeenit2000 · Jan 15, 2019 at 10:21 AM · unity 2dwaitforsecondscoroutinesienumeratorspawning problems

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.,

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jan 15, 2019 at 11:41 AM 0
Share

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.

avatar image tormentoarmagedoom · Jan 15, 2019 at 11:53 AM 1
Share

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!

avatar image Casiell · Jan 15, 2019 at 12:17 PM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharmajeenit2000 · Jan 16, 2019 at 06:47 AM 0
Share

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.

avatar image surfuay sharmajeenit2000 · Jan 18, 2019 at 02:11 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges