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 Thet Naing Swe · Aug 24, 2010 at 03:10 PM · yield

How can I use yield in C#

Why C# is lacking Javascript features in Unity.. How can I use the similar coding like below in C#?

print ("Starting " + Time.time); // Start function WaitAndPrint as a coroutine yield WaitAndPrint(); print ("Done " + Time.time);

function WaitAndPrint () { // suspend execution for 5 seconds yield WaitForSeconds (5); print ("WaitAndPrint "+ Time.time); }

when I try to call yield in C#, it always say that feature is not supported in C#..

Comment
Add comment · Show 1
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 Wolfram · Aug 24, 2010 at 03:18 PM 0
Share

I suggest reading the documentation, and searching this site, before asking questions. You will find plenty of information and answers. I'll even throw in a hint: "StartCoroutine"

3 Replies

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

Answer by Mike 3 · Aug 24, 2010 at 03:24 PM

This should do it, added a couple comments to describe what's needed:

Debug.Log ("Starting " + Time.time); // Start function WaitAndPrint as a coroutine //you need to explicitly tell it to start the coroutine - javascript does this implicitly (most of the time) yield return StartCoroutine(WaitAndPrint()); Debug.Log ("Done " + Time.time);

//the function return type needs to be IEnumerator (javascript does this implicitly) - you'll need to have using System.Collections; up at the top of your code IEnumerator WaitAndPrint () { // suspend execution for 5 seconds yield return new WaitForSeconds (5); Debug.Log ("WaitAndPrint "+ Time.time); }

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 Tabu · Aug 27, 2010 at 08:31 PM 0
Share

Thank you so very much!

avatar image
2

Answer by Tetrad · Aug 24, 2010 at 07:11 PM

yield isn't just for coroutines either.

Try doing this in Javascript.

IEnumerator CardinalDirections() { yield return "North"; yield return "South"; yield return "East"; yield return "West"; }

// another function somewhere foreach( string direction in CardinalDirections() ) { Debug.Log( direction ); }

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 Mike 3 · Aug 24, 2010 at 07:51 PM 0
Share

Works fine in Javascript too if you use the right syntax.

avatar image
1

Answer by equalsequals · Aug 24, 2010 at 03:28 PM

I wrote a tutorial on this a while back. liiiiiink

Cheers

==

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 Mike 3 · Aug 24, 2010 at 03:59 PM 0
Share

You may want to modify the linked article - you keep mentioning coroutines being asynchronous and running parallel, neither of which are true (They're run completely synchronously with the main thread)

avatar image Thet Naing Swe · Aug 26, 2010 at 05:29 AM 0
Share

That's this article is also useful for working with CoRoutine..

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

C# WaitForSeconds doesn't seem to work ?? 2 Answers

Last item not being moved (yield problem) 0 Answers

Why is my delay not working? 2 Answers

INTERNAL COMPILER ERROR 0 Answers

How can I use a co-routine to yield player damage? 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