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 Simon V · Apr 22, 2012 at 02:56 PM · c#coroutineyield

Yield not working properly

I'm working on a pathfinding/waypoints script. I move from waypoint to waypoint with a loop, that does a linear interpolation and yields every frame. This works perfectly, until I reach the first waypoint. After that, it will execute the whole loop without yielding - or at least, that's what I've observed through debugging.

 void Update() 
 {
     currentPosition = transform.position;
     targetPosition = waypoints[currentWaypoint].waypoint.position;    
     
     StartCoroutine(GetNextPos());
     
     if (currentPosition == targetPosition)
     {
         currentWaypoint = Mathf.Clamp(currentWaypoint + 1, 0, waypoints.Length - 1);
         startPosition = currentPosition;
         targetPosition = waypoints[currentWaypoint].waypoint.position;
     }
 }
 
 IEnumerator GetNextPos()
 {
     float x = 0f;
     while (x <= 1.0f)
     {
         x += Time.deltaTime * speed;
         transform.position = Vector3.Lerp(startPosition, targetPosition, x);
         yield return null;
     }
 }
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 AchillesWF · Apr 22, 2012 at 03:16 PM 0
Share

Not sure, but you may need to yield to GetNextPos() like so:

yield return StartCoroutine(GetNextPos());

This page explains the difference:

http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

avatar image Kryptos · Apr 22, 2012 at 03:20 PM 0
Share

@AchillesWF you cannot yield inside Update. But you can certainly do it inside Start, which is what I propose in my answer.

avatar image AchillesWF · Apr 22, 2012 at 03:26 PM 0
Share

Thank you for the correction. Yes, of course to use yield the return value of the function must be IEnumerator and Update() therefore does not qualify.

1 Reply

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

Answer by Kryptos · Apr 22, 2012 at 03:13 PM

It is really a bad idea to call StartCoroutine inside Update() without proper control. Each frame you will start a new coroutine which is not what you want. Because after 50 frames you will have 50 coroutines that are executed concurrently.

I don't know exactly what you want to do, but consider this kind of solution instead that use the Start() method which here is also a coroutine:

 IEnumerator Start()
 {
     while (aCondition)
     {
         yield return StartCoroutine(GetNextPos());

         aCondition = (some test and value check);
     }
 }

 IEnumerator GetNextPos()
 {
     // your code here

     // populate some member variable to help decide when to start this coroutine again
 }


edit: in your case aCondition will be to check whether you reached the last waypoint. And between two calls to GetNextPos(), you can set the start/end values, the same way you do inside the if statement in your Update() method.

Comment
Add comment · Show 5 · 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 Simon V · Apr 22, 2012 at 03:23 PM 0
Share

I do need this in my Update(), because I have to update other variables as well. If it helps explaining, it's for a tower defence game.

Also, what's the point of returning my coroutine itself? EDIT: never $$anonymous$$d, didn't notice Start was a coroutine as well.

avatar image Kryptos · Apr 22, 2012 at 03:25 PM 0
Share

As I explain in my answer. Start can be a coroutine and act like an Update method but with yield statements. This is the good way to do it when you use coroutines.

Also, what's the point of returning my coroutine itself?

That's how coroutines work. Read more carefully the documentation and check some tutorials (for example the one about states machines).

avatar image Simon V · Apr 22, 2012 at 03:34 PM 0
Share

It's working! Still need to tweak it a bit, as it's doing each waypoint twice for some reason. But at least I can make some progress now, thank you!

avatar image Eric5h5 · Apr 22, 2012 at 03:34 PM 0
Share

@Simon V: it's highly likely you don't actually need to use Update. You can probably restructure your code to eli$$anonymous$$ate Update in favor of using coroutines, and it will make things more understandable.

avatar image Simon V · Apr 22, 2012 at 03:49 PM 0
Share

@eric5h5: Yep, I've figured it out by now :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How To cycle coroutine in C# say every 1 sec? 2 Answers

[C#]Spawnmanager loop 2 Answers

Playing footsteps with interval 1 Answer

In coroutin function,if(x) { yield return a } . if(y) { yield return b }. Is it possible? 0 Answers

C# Wait for Coroutine 3 Answers


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