• 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 jessee03 · Oct 21, 2011 at 03:58 AM · timers

Resetting wait for seconds

My code has a bunch of waitForSeconds functions that control the spawning of objects. My problem is when I use a restart function the waitForseconds function is still running even if I pause that piece of code. So when things start repawning it's a lot more than it should be... I'm not sure if it's possible to reset any of these waitForSeconds timers ?

Comment
Add comment · Show 4
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 CarlLawl · Oct 21, 2011 at 01:31 PM 0
Share

put the function call in an if statement or a for loop? if you paste your code ill take a look at it

avatar image jessee03 · Oct 21, 2011 at 02:43 PM 0
Share

Well I have a ton of code, I'll post an example of what my issue is.

function spawn() {

if(gameIsOver == false) {

yield WaitForSeconds(Random.Range(40,60));

spawnStuff();

}

}

Alright so here is some basic code. The problem is if the waitForSeconds is still waiting after the gameIsOver variable is switched to True, here is where issues happens. As soon as the game resumes after being switched back to gameIsOver = false; this spawning code I believe duplicates itself and finishes the previous amount of time it was waiting for. I'm just guessing because I have like 3-4 objects spawning as soon as I click the retry button and all the variables in the game are reset after your game over so you can begin playing again.

avatar image DavidDebnar · Oct 21, 2011 at 02:50 PM 0
Share

you should make a variable and ask for it like if(!gameIsOver && canSpawn) and then set canSpawn to false and then set it again to true at the end of spawnStuf() function.

  • David

avatar image jessee03 · Oct 21, 2011 at 03:18 PM 0
Share

In my current code I do have it setup so that the function won't be recalled if gamIsOver = true; If I reset everything before that timer is up then it screws up it's spawning rotation completly. I don't think there's anyway to reset all the waitForSeconds timers so I don't have this issue.

1 Reply

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

Answer by aldonaletto · Oct 21, 2011 at 03:59 PM

If you want to stop all coroutines currently running, you can try StopAllCoroutines() - I've not used this before, but I suppose this function can kill all of them.
But if your problem is new coroutines being started while the older ones have not finished yet, the best solution (always recommended with coroutines) is to have a boolean flag for each process: 1- only start a new cycle if the flag is false; 2- set flag to true at the beginning; 3- reset flag to false at the end. Something like this:

var spawnFlag: boolean = false; // boolean flag for this coroutine

function spawn() { // only starts a new cycle when the last one ended if (gameIsOver == false && spawnFlag == false){ spawnFlag = true; // new cycle started... yield WaitForSeconds(Random.Range(40,60)); spawnStuff(); spawnFlag = false; // cycle ended } }

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 jessee03 · Oct 21, 2011 at 04:18 PM 0
Share

This is currently how I have everything setup to stop things from spawning. Even if you stop the code from running, when it starts up again it will still spawn a bunch of items. I need a way to change all the waitForSeconds to null or something like that.

avatar image jessee03 · Oct 21, 2011 at 04:28 PM 0
Share

nvm seems like StopAllCoroutines(); fixed the issue !! Thank you so much !

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with Time.time 0 Answers

How to make a stopwatch that restarts every time the player dies and saves it as a highscore 2 Answers

How to create a UDK-like timer to call a function ? 1 Answer

Timer Data Across Scene Changes 1 Answer

adding a timer to a GUIText toggle input button 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges