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 Reticulatas · Sep 01, 2011 at 03:34 AM · c#coroutinetimeclassyield

C# yield not doing anything

I have two classes, ClassA and ClassB.

In Class B there is this method (cA is a reference to class A):

 public void Entrance()
 {    
       StartCoroutine(cA.DoStuff());      
 }

In Class A is the do stuff method:

 public IEnumerator DoStuff()
 {
     Debug.Log(Time.time);
     yield return new WaitForSeconds(2);
     Debug.Log(Time.time);
 }

Both times come out the same, even though they should be two seconds apart. Both classes are Monobehaviours and the contents of the methods work fine. They do their job, its just that it takes awhile and I would like to keep the rest of the game updating. But currently it seems the yields are just ignored, what gives?

EDIT:

DoStuff needed to be started from a method in it's own script. A dummy middleman solved that. The real problem arose when I did that. The equivalent DoStuff() in my script is called around 100 times within the same frame. This freezes the game as if you didn't even have the yield in there. Thus all the DoStuffs() have the same start and end time because they are running parallel to each other.

I could fix this with a 'yield return StartCoroutine' but this would require the middleman to be of type IEnumerator, which would bring me back to my original problem of not being able to call the method from another script.

So in order to yield you need IEnumerator and to call the method with the yield you need an IEnumerator, then to call that method... so on and so on.

How do you break this endless chain of Enumerators?

EDIT 2:

I found a workaround which does not use yield.

Still, here's a (hopefully) more specific explanation of what I am doing: There are two classes, one that generates the locations for hundreds of GameObjects and stores them in an array in the other class.

Class A is the Generator

Class B is the Renderer and handles all displaying/retrieving/manipulating of the GameObjects

Before the first update is ever called, The Generator (A) goes to work placing references to all these GameObjects and Renderer (B) begins Instantiate what the Generator says to make. This takes a long time and freezes the game during the process.

If I threw a yield every time the Renderer finished a task (each one taking around 0.2 seconds processing time), the game would not freeze. That's the situation.

I can put a yield in but the resulting Coroutine code causes there to be so many Coroutines running that it might as well not have any yields.

My workaround is fairly simple: The Generator adds the GameObjects to be Instantiated to a queue and then the Renderer makes a bunch every time Update is called.

There may not be a clean answer to this problem. I'm sorry if it's a bit hard to understand what I wrote.

Comment
Add comment · Show 6
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 Eric5h5 · Sep 01, 2011 at 08:35 PM 1
Share

DoStuff doesn't need to be started from a method in its own script. It's not necessary to have middlemen or chains of IEnumerators. You must have some other issue. Also, why are you calling DoStuff 100 times? Perhaps you should post the actual scripts, because what you wrote here works perfectly fine.

avatar image Reticulatas · Sep 01, 2011 at 08:39 PM 0
Share

As far as I can see, only using StartCoroutine() will continue execution ins$$anonymous$$d of waiting for the coroutine to finish. To circumvent this we use yield return StartCoroutine() which requires the method it is contained in to be of type IEnumerator. Somewhere along the line one of the methods must run concurrently with the coroutine

avatar image Eric5h5 · Sep 01, 2011 at 09:13 PM 1
Share

$$anonymous$$aybe you should explain what you're actually trying to do.

avatar image Reticulatas · Sep 01, 2011 at 09:31 PM 0
Share

edited my post. Thanks

avatar image Owen-Reynolds · Sep 02, 2011 at 02:50 AM 0
Share

So, you're doing a pile of pre-processing for stage 2 in spare cycles of stage 1? Seems like a single coroutine with a 1-frame yield would be fine.

If creating an item really takes 2/10th of a second, yow! Is there anything in common you can precompute? There's no faster algorithm?

Show more comments

2 Replies

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

Answer by Eric5h5 · Sep 01, 2011 at 05:15 AM

If I run those scripts (replacing "Entrance" with "Start" and creating an appropriate cA variable for the first script), then the output is:

 0
 2.004005

So in fact everything works as expected.

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
avatar image
0

Answer by Owen-Reynolds · Sep 01, 2011 at 07:38 AM

There could be an issue with starting a coroutine in a different script. If so, can work-around by converting DoStuff into a "driver":

 // in "other script" A. Users call this one with cA.DoStuff();
 public void DoStuff() { StartCoroutine( realDoStuff(); }

 private IEnumerator realDoStuff() { print, yield, print .... }
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 Reticulatas · Sep 01, 2011 at 02:09 PM 0
Share

Yes, this is in fact the problem. Looks like Unity didn't like calling coroutines in different scripts. But this has opened another can of worms, par my edit.

avatar image Eric5h5 · Sep 01, 2011 at 08:33 PM 1
Share

@Reticulatas: No, as I said in my answer, I replicated your scripts and they work fine. Calling a coroutine in a different script is not an issue. I do it with some frequency.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Custom Yield Instruction for FixedUpdate frames 3 Answers

Stop the Awake() until a user clicks a button? 1 Answer

C#: Use of while loops inside of a coroutine 2 Answers

How to use yield within a class function 2 Answers


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