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
0
Question by Polantaris · Sep 20, 2013 at 03:26 AM · yieldjs - c#

Issue with yield instructions in C#.

I am attempting to re-write a lot of my scripts into C#, and I am having issues getting Yield Instructions to function how I wish them to.

No matter how I attempt to execute them, they either cause the methods they are used in to continue to the next method without being finished, or completely crashes Unity on attempt to execute the script.

I have tried:

 yield return null;
 yield return new WaitForSeconds(0.001f);
 yield return new WaitForEndOfFrame();
 yield return 0;
 yield return StartCoroutine(WaitMethod(0.001f); //This would simply run the WaitForSeconds in another method.

All in locations where I would like to pause temporarily. They mostly cause Unity to just completely freeze. I do not want to pause very long, for milliseconds just so that Unity can perform as normal, as they are long scripts that take some time to execute.

I know that EVERY method I am attempting these in work normally if they are not yielded at all, but they take too long to execute in one burst and cause them to freeze the game for seconds or more, and because of how often they run it is not acceptable (for example, pathfinding or dynamic generation).

I do not want to create separate Coroutines, as one method relies on the next. The use of the yield functions is simply to pause them for a very short period so that everything else can function.

Any help on this issue would be great. I had these all working in JS, but Yield Instructions in C# seem to work differently so I'm not sure what I'm 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

1 Reply

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

Answer by TrickyHandz · Sep 20, 2013 at 03:32 AM

With C# anything with a yield needs to be within an IEnumerator (i.e. a Coroutine), so you would have to write coroutines for anything that requires a yield.

Comment
Add comment · Show 6 · 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 Polantaris · Sep 20, 2013 at 03:43 AM 0
Share

I have made all the methods that would need a yield into a Coroutine at some point during testing yield instructions, however whenever I execute the yield in that method, it causes all of Unity to just freeze.

So, for example:

 IEnumerator<WaitForSeconds> $$anonymous$$ethod() {
   for(some loop) {
     do things;
     yield return new WaitForSeconds(0.01f);
   }
 }


That function causes Unity to crash completely and utterly when attempted to run, compared to:

 void $$anonymous$$ethod() {
   for(some loop) {
     do things;
   }
 }


This second function will run perfectly fine, but just take some time due to the nature of what it needs to perform.

avatar image TrickyHandz · Sep 20, 2013 at 04:28 AM 1
Share

As far as I know an IEnumerator does not take a generic parameter. The syntax for your Coroutine should be like this:

 IEnumerator $$anonymous$$yCoroutine()
 {
   for(some loop)
   {
     //Stuff to do
     yield return new WaitForSeconds(0.01f);
   }
 }
avatar image Polantaris · Sep 20, 2013 at 04:44 AM 0
Share

I read that a lot recently, and have been trying to figure out why I have been getting an error when I attempt to use just "IEnumerator".

Turns out it was because I was including System.Collections.Generic (and not System.Collections by itself, because I didn't need it), and there is a Generic version of IEnumerator. This is likely the cause of my crashing, I will test this now.

Thanks a lot.

avatar image TrickyHandz · Sep 20, 2013 at 04:47 AM 1
Share

No problem, and thanks for pointing out that there is a Generic version of IEnumerator. That is something that I'll have to look into.

avatar image Polantaris · Sep 20, 2013 at 04:49 AM 0
Share

Yep, that seems to have been the problem. It's not completely working yet, but it's not crashing anymore so I can work it out from here. Thanks again, much appreciated.

Show more comments

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I use yield in C# 3 Answers

Yield not working in C# 2 Answers

Issue with yield 0 Answers

How to dispaly list of images from server on GUI? 1 Answer

Need to call yield TWICE ??? (ANSWERED) 3 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