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 SturmPanther · Dec 05, 2015 at 06:15 PM · coroutinecoroutinescoroutine errors

Coroutine not working - what I did wrong?

Can anyone explain to me why this coroutine won't work ? I used Start Coroutine and it builds fine, but It doesn't wait for the seconds.

 public class count : MonoBehaviour {
 
     public int counter = 0;
 
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         StartCoroutine (countUpdate());
     }
     IEnumerator countUpdate()
     {
         counter += 135;
         yield return new WaitForSeconds(1f);
         Debug.Log (counter);
     }
 }
 
Comment
Add comment · Show 2
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 gjf · Dec 05, 2015 at 05:01 PM 2
Share

starting coroutines in Update() is a bad idea. you state that it doesn't wait for the seconds - what does it do?

avatar image Salmjak · Dec 05, 2015 at 09:26 PM 1
Share

As gjf said. This code will start a coroutine every time Update() is called (I believe). If you want it to repeat you should put a while-loop inside the coroutine (while(true)?) and start it from the Start()-function.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by slimsterstanley · Dec 06, 2015 at 02:41 PM

Both guys are right.

I haven't tested it but you could just do this :

public class count : MonoBehaviour {

  public int counter = 0;
 
  // Use this for initialization
  void Start () 
  {
           InvokeRepeating("countUpdate", 0.0f, 1f);
  }
  
  void countUpdate()
  {
      counter += 135;

      Debug.Log (counter);
  }

}

Comment
Add comment · 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
0

Answer by Bunny83 · Dec 05, 2015 at 10:00 PM

Like @gif and @Salmjak said starting a coroutine every frame is not a good idea. The coroutine does work as expected. Each frame you start a new coroutine. Each coroutine will immediately add 135 to your counter variable. So you have an increase of 135 each frame. Each coroutine will wait for 1 second and after one second they print the current counter value. Since you start a new coroutine every frame after the first second the first started coroutine will complete and print the debug log. From now on each frame another coroutine will finish since you started a new one each frame.

What you might want to do is something like this:

 public class count : MonoBehaviour
 {
     public int counter = 0;
     void Start () 
     {
          StartCoroutine (countUpdate());
     }
 
     IEnumerator countUpdate()
     {
         while (true)
         {
             counter += 135;
             yield return new WaitForSeconds(1f);
             Debug.Log (counter);
         }
     }
 }

This way you start one coroutine which will loop forever. It will complete one iteration every second.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to stop a coroutine started in Script A from within Script B 1 Answer

Collection Change During Iteration 0 Answers

Coroutines not passing yield 1 Answer

Why doesn't a coroutine start executing from beginning when I start it again? 1 Answer

Coroutine Won't Stop Using IEnumerator 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