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
1
Question by juliobds · Aug 20, 2011 at 08:03 AM · coroutineyieldwaitforsecondsfor-loopwhile-loop

Understanding yield inside a for or a while loop

Hello guys,

I'm trying to understand how coroutines work. I've already read all of the documentation that unity has.

Here is what I'm trying to do:

         public void Awake()
         {
             StartCoroutine(cotest());
         }
 
         public IEnumerator cotest()
         {
             for (int x = 0; x <= 1000; x ++)
             {
                 for (int y = 0; y <= 1000; y ++)
                 {
                     //do something
                 }
                 Debug.Log("I'm in!!!");
                 yield return new WaitForSeconds(0.1f);
             }
             
         }


I want the text "I'm in!!!" to be shown on the console 1000 times. But it only shows once. It's has if the the code never comes back to continue the yield.

Can you guys help me? What am I doing wrong?

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
6
Best Answer

Answer by YikYikHeiHei · Aug 20, 2011 at 08:13 AM

I have to test your script, but it have to loop 1,2,3,4,5,etc.

I have to change the Debug.Log to test this

 using UnityEngine;
 using System.Collections;

 public class NewBehaviourScript : MonoBehaviour
 {

     public void Awake()
     {
         StartCoroutine(cotest());
     }

     public IEnumerator cotest()
     {
         for (int x = 0; x <= 1000; x ++)
         {
             for (int y = 0; y <= 1000; y ++)
             {
                 //do something
             }
             Debug.Log("I'm in!!! " + x); //<-- add a x to test this
             yield return new WaitForSeconds(0.1f);
         }

     }
 }
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 Joshua · Aug 20, 2011 at 08:16 AM 1
Share

That's because you also have collapse turned on, which collapses duplicate messages in the console, but are now changing the message each time.

avatar image juliobds · Aug 20, 2011 at 08:17 AM 0
Share

It worked. I guess the Debug.Log only show a new message if it's different from the last one.

Thank you very much :)

avatar image YikYikHeiHei · Aug 20, 2011 at 09:21 AM 0
Share

Who to vote me down...? :(

avatar image juliobds · Aug 20, 2011 at 02:35 PM 0
Share

It wasn't me for sure :(

avatar image YikYikHeiHei · Aug 20, 2011 at 03:32 PM 0
Share

Thanks juliobds. :)

avatar image
3

Answer by Joshua · Aug 20, 2011 at 08:15 AM

It works the way you expect it to work. Your problem is that you have "Collapse" turned on in the Console. It's a button on the left top of the console window. ;)

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 juliobds · Aug 20, 2011 at 08:19 AM 0
Share

Thanks for the extra info, I would thumb you up but the site is giving me an error :(

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

What is wrong with this use of WaitForSeconds? 1 Answer

fps calculation with while loop in coroutine 2 Answers

Why isn't this IEnumerator method being called? 1 Answer

Alternative to "yield return WaitForSeconds()" in Coroutine with .NET Framework (Unity 2018.4) 0 Answers

Coroutine sequence not running properly 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