- Home /
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.
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;
}
}
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.
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#
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.
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.
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.
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.
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.
Oh, I see. Thanks BY0LOG1C. Learn something new everyday, that's why I like this place.
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
Follow this Question
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