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 liormalichi · Mar 25, 2019 at 11:11 AM · coroutineienumeratoronceienumerable

StartCoroutine works once

Hi!

having a problem with StartCoroutine, it seems like it works only once.

the purpose of the code is to show 3..2...1... and then start the game after the player clicked resume (which is after he paused the game previously obviously) .

my code

     if (resume.paused || restart.continueGame1)
     {
         StartCoroutine(smoothBack());

         if (continueGame)
         { 
             Time.timeScale = 1f;
             resume.paused = false;
             restart.continueGame1 = false;
             continueGame = false;

         }




IEnumerator smoothBack() {

     Time.timeScale = scale;
     three.SetActive(true);

     yield return new WaitForSeconds( scale);

     three.SetActive(false);

     two.SetActive(true);

     yield return new WaitForSeconds( scale);
     two.SetActive(false);

     one.SetActive(true);

     yield return new WaitForSeconds( scale);
     one.SetActive(false);
 
     continueGame = true;

 }



for the first time everything runs great, but when I try to run the" smoothBack() " for the second time and beyond it doesn't work.

need your help! thanks!

Comment
Add comment · Show 5
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 liormalichi · Mar 25, 2019 at 11:23 AM 0
Share

public class resume : $$anonymous$$onoBehaviour { public GameObject pauseCanvas; public static bool paused = false;

    public void resume1()
  {



     
     paused = true;

  }

}

avatar image Bonfire-Boy · Mar 25, 2019 at 11:29 AM 0
Share

What do you mean by "it doesn't work"? Does it get called? Add some logging if you're not sure.

Without seeing the whole of the first function, or knowing how/when it's called, it's hard to be sure... but there's something suspicious about what you have shown. It looks like the "if (continueGame)" bit is supposed to happen after smoothBack has returned, but it will in fact happen the first time that smoothBack yields.

avatar image liormalichi Bonfire-Boy · Mar 25, 2019 at 11:40 AM 0
Share

by it doesn't work I mean the game calls the function however it display the 3...2...1.. for a split of a second ins$$anonymous$$d of three seconds as it was for the first time.

getting in the "if (continueGame)" conditional, spouses to happen only after displaying the 3..2...1.. and that's what is happing but, again, there are shown for only split of a second

avatar image Bonfire-Boy liormalichi · Mar 25, 2019 at 11:58 AM 1
Share

As I suspected, it looks like you're misunderstanding how coroutines work. Your first function will not wait for smoothBack to finish. It does not "only happen after displaying the 3..2...1", it happens as soon as you display the "3" (because that's when the coroutine first yields). So as soon as you display the "3", Time.timescale is set back to 1 and so things happen faster than you want.

If you want to wait for smoothBack to finish before setting timescale back to 1, there are (at least) 3 approaches you can take:

1) turn the first function into a coroutine and use yield return StartCoroutine(smoothBack) in it,

2) moved the contents of the if (continueGame) block into another function which you call at the end of smoothBack.

3) move the stuff in the if (continueGame) block to the end of the smoothBack function.

Show more comments

1 Reply

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

Answer by Bunny83 · Mar 25, 2019 at 12:46 PM

Your issue is line 7 in your first code snippet:

 Time.timeScale = 1f;

When you call StartCoroutine your coroutine will immediately run up to the first yield statement before the StartCoroutine call returns. That means your coroutine has already set Time.timeScale to "scale". However when you come back and "continueGame" is true you set the Time.timeScale back to 1. Since you probably intended to set the timeScale to some very small (not 0) number that means each of the WaitForSeconds will only wait for one frame since you set the timescale back to 1.


Just remove that line and you should be fine. Though the coroutine should set the timeScale back to 1 at the end.


You don't seem to need your "continueGame " bool at all. It look like you assumed that StartCoroutine will somehow "wait" until the coroutine is finished. That's not true. The code after your StartCoroutine call runs immediately after the coroutine hits the first yield. That's why your first call kind of worked. It never entered the if statement. However you started a new coroutine every frame. Your code of starting the coroutine should look like this:

 if (resume.paused || restart.continueGame1)
 {
     resume.paused = false; // ensures we only start the Coroutine once
     restart.continueGame1 = false;  // ensures we only start the Coroutine once
     StartCoroutine(smoothBack());
 }

And at the end of your coroutine you should set the timescale back to 1

     // [ ... ]
     yield return new WaitForSeconds( scale);
     one.SetActive(false);
     Time.timeScale = 1f;
 }

Though instead of using that "tiny scale trick" you can now simply use WaitForSecondsRealtime. This does also work when the timescale is set to "0".

Comment
Add comment · Show 1 · 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 liormalichi · Mar 25, 2019 at 01:06 PM 0
Share

Amazing thanks

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

112 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Corutine not behaving like it should? 1 Answer

Returning an IEnumerator as an int? 1 Answer

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers

Coroutoutine doesnt work properly. 3 Answers

How to have IEnumerators run but not in Update 2 Answers


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