Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by AntLewis · Apr 02, 2015 at 04:45 PM · coroutine

Coroutine and Loops

Hi there, I'm using coroutines to drive a character behaviour:

     void Start ()
     {
         m_rigidbody = GetComponent<Rigidbody2D> ();
         m_characterProperties = GetComponent<CharacterProperties> ();
          
         StartCoroutine (StartBehaviour ());
  
     }
 
     IEnumerator StartBehaviour ()
     {
 
         m_timer = 0f;
  
             while (m_timer < 2.0f) {
                 m_timer += Time.deltaTime;
                 yield return null;
             }
             StartCoroutine (Check ());
 
     }

So in the above example, I wait two seconds then call the Check() coroutine (not shown for clarity) - this all works fine. However, I basically want to loop this behaviour - so I want the logic to repeat (so wait two seconds, then call Check again, repeat). However, I'm a little lost how to best do this - any suggestions? Thanks!

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

2 Replies

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

Answer by Eno-Khaon · Apr 02, 2015 at 05:26 PM

Here's a heavy-handed way of approaching it:

 IEnumerator StartBehaviour ()
 {
     while(true) {
          m_timer = 0f;
  
          while (m_timer < 2.0f) {
              m_timer += Time.deltaTime;
              yield return null;
          }
          StartCoroutine (Check ());
          m_timer = 0.0f;
          yield return null;
      }
 
 }

That way, the "StartBehaviour" Coroutine continues endlessly and the timer resets every time it's successful.

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 Bunny83 · Apr 02, 2015 at 05:35 PM 0
Share

Dependign on what Check actually does, you might want to use

 yield return StartCoroutine (Check ());

Which will make the StartBehaviour coroutine to wait for the Check coroutine to finish before it restarts the loop. Otherwise if Check needs for example 1.5 seconds to complete, a new Check will be started 0.5 seconds after the old one has finished. If you yield the coroutine it will start the 2 seconds waiting time after Check has finished.

If Check would actually take longer than 2 seconds to finish, 3 sec. for example, StartBehaviour would start a new Check coroutine event the old one hasn't finished yet. So for 1 sec. There would run two Check coroutines at once. Depending on what the coroutine does this might be a huge problem.

avatar image AntLewis · Apr 03, 2015 at 11:21 AM 0
Share

Thanks for all the responses guys - made everything a little clearer!

avatar image
1

Answer by UNZoOM · Apr 02, 2015 at 04:53 PM

If you dont want to stick to coroutine then follwoing will work. Wait for 2 seconds and call Check func , followed by repeated call every 2 seconds.

 InvokeRepeating("Check", 2,2);

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 AntLewis · Apr 02, 2015 at 05:15 PM 0
Share

I was specifically wanting to use coroutines - but thanks for the response!

avatar image UNZoOM · Apr 02, 2015 at 05:23 PM 0
Share

w.r.t coroutines..

  void Start ()
      {
          StartCoroutine("StartBehaviour ");
      }
      
      IEnumerator StartBehaviour ()
      {
          while(true)
          {
              yield return new WaitForSeconds(2);
                StartCoroutine (Check ());
          }
      }

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

21 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

Related Questions

LoadLevel*Aynsc() and Coroutine 3 Answers

Execution manner of co routine 2 Answers

Issue With Simultaneous ReadPixels Calls 0 Answers

How to Substitute Coroutine in non-MonoBehaviour Class? 1 Answer

Working projectile scripts do not work due to if condition 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