• 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 GutoThomas · May 16, 2012 at 05:02 AM · coroutineloopyieldreturn

Pause the coroutine inside a loop

Is it possible to wait for x seconds between every item in the for loop?

Like:

 for(int i = 0; i < 4; i ++) {
 
 Debug.Log(i.ToString());
 
 yield return new WaitForSeconds(1);
 
 }

This would works like so: do the code of the for one time, pause the corotine until WaitForSeconds is complete.

When I did this it's just printing 0, 1, 2, 3 every frame. I want it to print:

0...(wait 1 second)...1...(wait 1 more second)...2... and so on...

Any help would be appreciated!

Thanks from now!

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

1 Reply

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

Answer by syclamoth · May 16, 2012 at 05:09 AM

The above code must have been called via a 'StartCoroutine' function, otherwise it won't be used by the MonoBehaviour's coroutine scheduler, and will all run at once!

Hence:

 IEnumerator PrintNumbers(int countUpTo, float waitTime)
 {
     for(int i = 0; i < countUpTo; i ++) {
         Debug.Log(i.ToString());
         yield return new WaitForSeconds(waitTime);
     }
 }

 // elsewhere...

 StartCoroutine(PrintNumbers(4, 1)); // Prints your numbers, waits!
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 GutoThomas · May 16, 2012 at 05:24 AM 0
Share

Thank you! Worked as I wanted!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I return a value from a coroutine? 8 Answers

While loop yield lag? 1 Answer

How do I return a reference to an object downloaded via a function? 0 Answers

Re-using the "same" for loop? 1 Answer

Running multiple coroutines in loops with different time 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges