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
4
Question by darthbator · Oct 15, 2012 at 11:37 PM · c#coroutineyieldwhile

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.....

Comment
Add comment · Show 3
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 Owen-Reynolds · Oct 16, 2012 at 01:17 AM 0
Share

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.

avatar image MarkFinn · Oct 16, 2012 at 05:34 AM 0
Share

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.

avatar image Fattie · Oct 16, 2012 at 01:53 PM 0
Share

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.

2 Replies

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

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.

Comment
Add comment · Show 7 · 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 darthbator · Oct 16, 2012 at 05:48 AM 0
Share

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?

avatar image Eric5h5 · Oct 16, 2012 at 07:45 AM 2
Share

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.

avatar image Kryptos · Oct 16, 2012 at 08:59 AM 0
Share

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.

avatar image Fattie · Oct 16, 2012 at 01:57 PM 1
Share

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 :)

avatar image ecesis_llc · Aug 03, 2015 at 06:23 AM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · 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

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

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


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