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
4
Question by Luna4Dev1 · Jan 28, 2015 at 11:43 AM · c#coroutinecoroutines

Running a coroutine without StartCoroutine

I use coroutines for a lot of my initialization code, yielding frequently to reduce stutter on loading screens, etc.

However, sometimes I want all of my initialization to happen "all at once" (i.e in a blocking fashion, like a normal method, all in one frame). Previously, I have been duplicating my initialization code into coroutine and non-coroutine methods - I'd like to avoid this so I can reduce the amount of code I need to test and debug.

I've discovered that I can "run" the coroutine by iterating through it like a normal Enumerator - see code below.

Is what I'm doing supported? I can't see any disadvantages, or anything that will break, so far in my testing.

Thanks!

     void Start()
     {
         IEnumerator enumerator = MyCoroutine();
         while(enumerator.MoveNext())
         {
         }
         Debug.LogError("Complete");
     }
 
 
     IEnumerator MyCoroutine()
     {
         Debug.LogError("Coroutine Start");
         yield return new WaitForSeconds(3.0f);
         Debug.LogError("Coroutine End");
     }

Comment
Add comment · Show 3
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 Luna4Dev1 · Jan 28, 2015 at 10:42 AM 0
Share

It seems that yielding to a StartCoroutine() call in your Enumerator coroutine means that the child coroutine will behave as if it were started separately; e.g. it will run while yielding, not all at once like the parent coroutine.

So the above technique will work as long as you don't start coroutines in the IEnumerator that you are running

avatar image Sisso · Jan 28, 2015 at 11:46 AM 0
Share

This code should interest you http://wiki.unity3d.com/index.php?title=CoroutineScheduler

avatar image Luna4Dev1 · Jan 28, 2015 at 01:39 PM 0
Share

Looks interesting, Sisso. However I'd rather not include an entire new coroutine system just for my own convenience on a few initialization routines - I think I'll stick with my duplicated code for now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Jan 28, 2015 at 02:03 PM

The problem is that it open a door for possible bugs. Each coroutine return type have a meaning, and you are not respecting it. For example, I can insert yield return 0 after create many objects so I only continue after everything get fully initialized.

If is not causing problem and you know what are you doing, go on, no time to waste :P

In the long term a proper abstraction for lazy load should be a better idea, so you can control loads per time, have progress, retry, etc.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Loop Coroutine For A Demo Mode 1 Answer

Distribute terrain in zones 3 Answers

Is there a way to find out when a coroutine is done executing? 3 Answers

Two coroutines in Start(), must exec consecutively 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