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 ZowPac · Apr 06, 2010 at 04:52 AM · javascriptcoroutineyield

Confused about Coroutines

I'm almost convinced I'm seeing a bug, but this is an entirely new concept for me, so I need some help to understand what's going on. Here's a somewhat simplified version:

static function Initialize() { Debug.Log("EventManager.Initialize() called"); if (initialized) return; Debug.Log("EventManager.Initialize() 1"); eventQueue = new PriorityQueue(); initialized = true; Debug.Log("EventManager.Initialize() 2"); DoLoop(); Debug.Log("EventManager.Initialize() 3"); }

static private function DoLoop() { Debug.Log("Enter Loop()"); while (true) { Debug.Log("Top of Loop() loop"); while (eventQueue.Count) { // do something useful } Debug.Log("Before Loop() wait"); yield WaitForSeconds(1.0); } Debug.Log("Exit Loop()"); }

What's weird is I never see "Enter Loop()" in the console (or anything from DoLoop()), but I see the "Initialize 1", 2, and 3. If I comment out the while (true) loop, I'll see both the "Enter Loop()" and "Exit Loop()", but otherwise neither. What's going on?

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

Answer by KvanteTore · Apr 06, 2010 at 07:06 AM

When a coroutine is called, it does not automatically run through to completion. It returns the body of the routine embedded in an object implementing the IEnumerator interface. Each time MoveNext is called on the object, the coroutine continues until the next yield, and returns the yield argument (in your case an instance of WaitForSeconds).

When writing scripts in C#, this is explicit. You have to start a corutine with StartCoroutine(coroutine());, where the IEnumerator is passed to the unity engine, and called at the appropriate times depending on the [YieldInstructions][2] returned from the IEnumerator.

In Javascript, I believe the compiler determines which routines are coroutines, and automatically calls StartCoroutine automatically, however apparently not from static routines.

TLDR; do as Eric suggested, remove both instances of static.

Comment
Add comment · Show 4 · 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 runevision ♦♦ · Apr 06, 2010 at 09:37 AM 0
Share

Re the scripting language name, it is consistently referred to as Javascript in the Unity manual and in other official written Unity materials.

avatar image KvanteTore · Apr 06, 2010 at 11:14 AM 0
Share

@Rune ok. thx :)

avatar image Eric5h5 · Apr 06, 2010 at 05:03 PM 1
Share

Javascript automatically uses StartCoroutine from anywhere, not just Update etc.

avatar image KvanteTore · Apr 06, 2010 at 11:11 PM 0
Share

@Eric5h5. Ok, that's kind of neat and kind of scary... updated the last statement to reflect this.

avatar image
3

Answer by Eric5h5 · Apr 06, 2010 at 05:24 AM

Remove both instances of "static".

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

No one has followed this question yet.

Related Questions

yield works, but yield WaitForSeconds does not? 1 Answer

Need to call yield TWICE ??? (ANSWERED) 3 Answers

Stuck while solving a Coroutine problem 0 Answers

Waypoint / Yield help 1 Answer

Coroutine a function within a loop? 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