• 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 Serotonina · Jun 01 at 11:19 AM · coroutinewaitforsecondswhile-loop

Coroutine loop not working

Hi everyone,

I'm currently stuck on a Coroutine loop that does not seem to work.

I've tried simplifying the loop to its essence to see what is wrong, but to no avail. Here's the code:

 void Start()
     {
         StartCoroutine(Timer());
     }
 
     IEnumerator Timer()
     {
         while (true)
         {
             Debug.Log("Here it works");
             yield return new WaitForSeconds(1f);
             Debug.Log("So does here");
         }
     }

Basically, the first Debug.Log shows on the console just the first time, while the second never appears.

I can't seem to find the issue, what am I missing?

Thanks in advance for the help.

Cheers

Comment
Add comment · Show 9
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 Caeser_21 · Jun 01 at 11:25 AM 0
Share

Are there any other scripts running in the background? or does the GameObject with the coroutine ever get destroyed?

avatar image Serotonina Caeser_21 · Jun 01 at 11:31 AM 0
Share

Hi Caeser,

no, the object is always active and there are no other scripts interfering with this Coroutine. Now I've tried to bypass it by starting the coroutine in Update with a boolean that switches on/off, but I don't think it would be a great idea in the long run.

Cheers

avatar image Caeser_21 Serotonina · Jun 01 at 11:45 AM 1
Share

Hmmm, just to clarify you aren't setting Time.timeScle = 0 anywhere right... Maybe try using a Timer instead of a Coroutine like :

 private float Timer;
 private float MaxTime = 1f;
 bool HasRunned =  false;
 bool IsFinished = false;
 
 private void Update()
 {
     if(!HasRunned)
     {
         //Code to be called before one second
        HasRunned = true;
     }
 
     if (!IsFinished)
     Timer += 1 * Time.deltaTime;
 
     if (Timer >= MaxTime)
     {
         //Code to be called after the one second
         IsFinished = true
     }
 }

This isn't a great solution but I think it works

Show more comments
avatar image hirenmakwana · Jun 01 at 11:41 AM 0
Share

What's the output that you're expecting??

avatar image Serotonina hirenmakwana · Jun 01 at 11:53 AM 0
Share

Hi hirenmakwana,

I'm expecting the Couroutine to run a loop in which some code runs before the WaitForSeconds and some other code runs after the WaitForSeconds. Right now it seems like it's stopping right at the WaitForSeconds. and does not run the second part or reiterate the first one.

1 Reply

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

Answer by xxmariofer · Jun 01 at 12:09 PM

A. You are deactivating the object with the script attached.

B. You are setting the Time.timeScale to 0

Comment
Add comment · Show 5 · 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 Serotonina · Jun 01 at 12:27 PM 0
Share

Hi xxmariofer,

neither of both, because

a) the object is always active (I've tried attaching another script to the object with another coroutine I was using before and it works perfectly); b) the Time.Timescale is currently 1 as everything else is working as intended (player movement, other coroutines and so on);

avatar image xxmariofer Serotonina · Jun 01 at 12:31 PM 0
Share

add a log to the update, if the log gets printed, you are 100% canceling the coroutine somewhere else. Remember calling StopAllCoroutines will also stop coroutines from other scripts attach to the same gameobject

 void Update()
 {
     Debug.Log("ENTERS");
 }
avatar image Serotonina xxmariofer · Jun 01 at 12:43 PM 0
Share

This is a new script that is not called by anything, also, I'm currently not using StopAllCoroutines but stopping them by name with a manager. Also, this is the only coroutine that uses this name.

Anyway, I found a solution with @Caeser_21 suggestion.

Thanks anyway for your help!

Show more comments

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

151 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 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

Understanding yield inside a for or a while loop 2 Answers

fps calculation with while loop in coroutine 2 Answers

Yield WaitForSeconds doesn't work, give syntax error 1 Answer

How can i get this Delay script to work? 1 Answer

WaitForSeconds causing function to return 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