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 Anxo · Mar 27, 2012 at 12:24 AM · c#yieldwhile

yield with while via C#

Hey guys, I hope everyone is working on something cool.

I am trying to write the following code in C# but cant figure out the syntax, if you know the syntax or better yet point me to a resource where I can look this type of stuff up in the future without bothering everyone, that would be great thanx.

This is a Javascript version of what I want to create in C#

 private var dead : boolean = false;
 
 function Start(){
   while(true){
     while(!dead) yield;
     Debug.Log("dead");
     dead = false;
   }
 }
 

And this is what I have in C# but as you can see, I am writing the yield part wrong.

 bool dead = false;
 void Start(){
   StartCoroutine(Run());
 }
 IEnumerator Run() {
   while(true){
     while(!dead) yield;????
     Debug.Log("dead");
     dead = false;
 }
 }

Thanx guys.

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

3 Replies

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

Answer by Eric5h5 · Mar 29, 2012 at 10:26 PM

 private bool dead = false;
 
 IEnumerator Start(){
   while(true){
     while(!dead) yield return null;
     Debug.Log("dead");
     dead = false;
   }
 }
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 Anxo · Mar 29, 2012 at 10:28 PM 0
Share

ahhh, thank you.

avatar image Anxo · Mar 30, 2012 at 12:37 AM 0
Share

Eric, if you had to look up that sytax, where would you go?

avatar image
1

Answer by by0log1c · Mar 27, 2012 at 12:27 AM

For the code given, you probably mean to write:

 yield return null;

otherwise, the Scripting Reference has example in all three languages for most docs.

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 Anxo · Mar 27, 2012 at 01:15 AM 0
Share

I am not sure what that does, yield return null; What I am trying to do is yield while dead does not equal to true. The Javascript does exactly what I want, I just want to do the same thing in C#

avatar image by0log1c · Mar 27, 2012 at 12:50 PM 0
Share

Did you at least try the example I gave you? Its the exact translation of your code, it'll wait for a single frame until your boolean is true. Also the Scripting Reference does have example for each language on the very page I listed... IEnumerator are a special type of method and you should understand them.

avatar image Berenger · Mar 27, 2012 at 02:39 PM 0
Share

If you want to fully understand coroutines, I suggest you read that. It's kind of dry so be prepared, but it's worth it.

avatar image Anxo · Mar 29, 2012 at 10:18 PM 0
Share

Hi Byologic, I am not saying that your answer is wrong. And I have read that ref page. However, I do not understand how it applies to my variable. yield return null; waits for one frame correct? but I do not wish for it to go on unless the varible changes from false to true. And if it does do that. how would I set that up.

if I say yield return null; it will continue there the next time I call it but i do not get the relation with my dead varible.

avatar image
0

Answer by IndieScapeGames · Mar 27, 2012 at 12:28 AM

Well first off, you're certainly not bothering anyone here. In fact, for my case, you're helping me become a better programmer by troubleshooting other people's code. It's actually a great learning exercise.

Let me help you with a great resource, http://unity3d.com/support/documentation/ScriptReference/index.html. This is the API of Unity.

Now, back to your question, the yield is wrong. I don't think you know exactly what the Yield is supposed to be doing (I say that but I could be wrong).

Just doing a quick search on the API I found this link: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

It's all about Coroutines and the yield you're using.

When I think of yield, I think of the command WaitForSeconds(). But from the looks of it, it does do other functions.

What would you like your code to be doing, and then perhaps I can help further.

Comment
Add comment · Show 3 · 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 by0log1c · Mar 27, 2012 at 12:33 AM 0
Share

Both works differently. The user wants to use the version that will yield for a single frame while you're pointing to the one that waits for a specified amount of time. It took me a while to figure out Enumerator but its quite easy really.

avatar image IndieScapeGames · Mar 27, 2012 at 12:35 AM 0
Share

Oh, I see. Thanks BY0LOG1C. Learn something new everyday, that's why I like this place.

avatar image Anxo · Mar 27, 2012 at 01:17 AM 0
Share

The Javascript shows what I want to do in C#. The Javascript will not move past the "while(!dead) yield;" line unless the boolean turns true. All I am trying to do is convert that to C#. Thank you guys for your effort. I am familiar with the scripting refs but it does not list what I am looking for.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

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

C# Wait for Coroutine 3 Answers

Mysteries of yield 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