- Home /
C#: Use of while loops inside of a coroutine
So I'm having a little issue using while loops inside of a coroutine. I think it could just be my poor understanding of coroutines in general. I have this real simple coroutine I have been calling in order to move my camera around and point it at various objects. The movement works pretty well however I don't really understand how to get code outside of the while loop inside the coroutine to run? Does this have to do with where I am yielding? Here's my code
IEnumerator repositionCam () {
while (cameraMovement < 1) {
cameraMovement += camFocusSpeed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, transform.position + offset, cameraMovement);
yield return null;
}
Debug.Log("This code never runs");
}
Why is that code down there never executing? How could I hook into something (another method, whatever) once I hit cameraMovement > 1.
I've tried placing the return outisde of the while loop but that seems to actually crash unity.....
The Lerp seems odd to me, but the while loop should finish just fine. So, probably just a generic loop bug. Check camFocusSpeed and try printing cam$$anonymous$$ovement inside the loop.
For that matter, since cam$$anonymous$$ove is a global, someone in Update could be keeping it from reaching 1.
This will destroy performance, so be sure to take it out after, but do add a Debug.Log("$$anonymous$$ovement = "+camera$$anonymous$$ovement); to the inside of the loop. Just to be sure the values are as expected. If camera$$anonymous$$ovement does reach 1 then it certainly should work as expected.
Dart, you have to understand what a "frame" is.
the whole system does one "cycle" of professing, then it starts again. it does this hundreds of times per second typically.
say you want to "move" something continuously across the screen....
what you do is move it a little bit every 'frame". right? overall you'll see it smoothly moving across the scene.
so you want to "do something every frame" ...
this is EXACTLY what the "yield" thingy is for in this sense.
you go ... while(1) { sdo omething .. yield }
the yield just makes you WAIT UNTIL THE NEXT FRA$$anonymous$$$$anonymous$$ so, you see that code fragment I just typed... that is EXACTLY how you make the computer "do something each frame"
so if I said to you, oh, let's do "blah" each frame
you'd say .. sure! so that's like this:
while(1) { blah yield }
you get it?
so when you ask "why yield not outside the while" that's sort of meaningless.
the "whole point" of yield is you use it with a while - with some sort of loop.
it's for when you want to do things "every frame". that's the whole object of yield, in this context
if you struggle to know what a "frame" is you'll struggle with yield
the crisis of understanding here is the nature of a "frame". once you totally knwo what the hell a "frame" is, you're golden.
Answer by Eric5h5 · Oct 16, 2012 at 01:33 AM
The while loop will continue as long as cameraMovement is less than 1. Once it's greater than or equal to 1, the loop ends and the rest of the code after that is executed. If something outside the coroutine prevents cameraMovement from exceeding 1, then the loop will never end.
So I found what was hanging up that variable. I was wondering however if you might be willing to shed some light as to why that yield needs to be inside of that while loop? Is it because update will need to iterate through that loop inside the coroutine and it get's "stuck" inside the coroutine unless I can return something out of that loop?
There is no Update involved here. The yield waits for a frame; if it's not inside the loop then everything is done inside one frame.
Consider yield to be a kind of breakpoint. It tells the coroutine scheduler to stop execution at this point. Then after one frame, the control flow will return to that point (i.e. pause the breakpoint) and continue.
right. the fact is it's just an idiom @kry. Say Unity happened to give Unityscript a control structure:
doThisEveryFrameUntil( condition )
{
block
}
that would be absolutely identical and nothing would change, just the way you type it. the key to actual understanding for @darth is to understand what the hell a frame is :)
This helped out an issue I had as well. For some reason I never bothered to put a break point inside the while to see if it was still running, until reading this post. I was working on a 2D project and forgot to force the z axis for the camera. The camera would never reach its location because of this and never completed the loop.
Answer by Humanhoney · Sep 08, 2016 at 11:29 AM
use
IEnumerator repositionCam () {
while (cameraMovement < 1) {
cameraMovement += camFocusSpeed * Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, transform.position + offset, cameraMovement);
yield return null;
}
yield return 0; //<<<<Here Added
//Debug.Log("This code never runs");
}
to prevent crashing app, if u about this one.
Your answer
Follow this Question
Related Questions
Coroutines and states 1 Answer
What is wrong with this use of WaitForSeconds? 1 Answer
Wait for Coroutine without yield? (C#) 1 Answer
yield with while via C# 3 Answers
Please help with While Loops 1 Answer