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 Vallar · Apr 21, 2016 at 07:31 PM · coroutineienumerator

Coroutine won't run sometimes and other times it runs.

Hello,

I have a coroutine that runs every time a level ends to load up the next level. In level 1, the coroutine runs smoothly, no problems and as expected (it doesn't depend on the NumberOfEnemies script below, the coroutine is called from somewhere else). However, in level 2, the coroutine runs the first line only and the rest is being ignored forever and I am not sure why.

I put some print out messages for debugging and it confirmed that the first line in the coroutine runs, the second line is never run.

Here is the coroutine:

     public IEnumerator WinLevel()
     {
         Time.timeScale = 0.3f;
         yield return new WaitForSeconds(1);
         Time.timeScale = 0.01f;
         CurrentPlayer.GetComponent<Player>().enabled = false;
         guiManager.GUIWin();
     }

Here is what calls the above coroutine:

 public void CheckLevelWinCondition()
     {
         if (GameManager.instance.NumberOfEnemies > 1)
         {
             GameManager.instance.NumberOfEnemies -= 1;
         }
         else if (GameManager.instance.NumberOfEnemies <= 1)
         {
             StartCoroutine(GameManager.instance.WinLevel());
         }
     }


Any help would be appreciated, thank you very much in advance.

EDIT: I understand that when timeScale is set to 0.3 I am not waiting a real second, but more than that. It is intentional :). But even after 20 seconds it still not run.

Comment
Add comment · Show 6
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 elenzil · Apr 21, 2016 at 08:25 PM 0
Share

interesting. could you include the exact print statements you used and the output ? what happens if you comment out the first timescale set ? what happens if you comment out the second one ? it might be interesting to print the current timescale as well at the top of the coroutine. it's suggestive that it works the first time but not the second. i'm wondering if Time.timeScale doesn't take effect immediately.

try adding a yield return null just before the yield return new WaitForSeconds(1). that would allow the rest of the system to deal w/ the new timescale before encountering the time-delay.

avatar image Vallar elenzil · Apr 21, 2016 at 08:47 PM 0
Share

I added Debug.Log("Ran"); at the top of the coroutine and Debug.Log("Second wait done"); after the yield return new WaitForSeconds(1);

The first level, I had the two statement printed out in the console. The second level, only "Ran" got printed and the other one as never printed out.

If I comment out the first line (the timeScale change to 0.3f) nothing happens. As if there is no code being run. If I comment the second line out (the wait for one second) I changed the code tot he below:

 public IEnumerator WinLevel()
     {
         Time.timeScale = 0.3f;
         //yield return new WaitForSeconds(1);
         Debug.Log("Start");
         Time.timeScale = 0.01f;
         CurrentPlayer.GetComponent<Player>().enabled = false;
         yield return new WaitForSeconds(0.1f);
         Debug.Log("It ran");
         gui$$anonymous$$anager.GUIWin();
     }

First level, ran smoothly, no problems. The second level only "Start" got printed out and the rest never happened.

Lastly I tried adding the yield return null and on the first level it worked pretty well while on the second nothing happened, same problem.

it might be interesting to print the current timescale as well at the top of the coroutine. it's suggestive that it works the first time but not the second.

Actually the timeScale change works. The first line runs with no problems in both levels but in the second level, that is all what runs. The timescale changes and nothing else is run in that block of code.

avatar image tanoshimi · Apr 21, 2016 at 08:43 PM 0
Share

What exactly is the point of those Time.timeScale statements? And does the coroutine work correctly if you comment them out?

avatar image Vallar tanoshimi · Apr 21, 2016 at 08:50 PM 0
Share

This is the win condition, I wanted to slow down the game to give the player a "heads up" the game has been won and transition "loss of control" rather than all of a sudden the game disables the controls and a win menu appears.

I commented the first timescale and in the first level it ran with no problems (like with the timescale set to 0.3f). In the second level it didn't run after the wait.

avatar image Jessespike · Apr 21, 2016 at 08:47 PM 0
Share

I don't think changing the global timescale is a good idea, even if its intentional. Ins$$anonymous$$d of:

      Time.timeScale = 0.3f;
      yield return new WaitForSeconds(1);

why not just wait 3 seconds?

If you have components that depend on a timescale, then maybe try making your own public static float to act as a timescale. I think there might be some internal Unity methods that run accordingly with the global timescale, so it might be better to leave it untouched.

avatar image Vallar Jessespike · Apr 21, 2016 at 08:50 PM 0
Share

Because I wanted to add a slow-motion effect not only the 3 seconds wait.

0 Replies

· Add your reply
  • Sort: 

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

43 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

Related Questions

How do I get into the data returned from a UnityWebRequest ? 2 Answers

Coroutines IEnumerator not working as expected 2 Answers

Returning an IEnumerator as an int? 1 Answer

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

IEnumerator not looping correctly? 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