Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Extrumus · May 18, 2019 at 12:44 PM · movementcoroutinemove an objectcoroutine errorspausing

Coroutine randomly stops and continues after pausing and unpausing game

I have a path fallowing script and this is the coroutine that is called when a path has been generated (before calling it stops it as well). The problem is when enemy (the one with this coroutine) is going through one of the many paths and randomly decides to stop. Only way to get it unstuck is with pausing the game and unpausing (Ctrl+Shift+P) then it runs a bit and the stops again. All of the lines of code are fired when it's stuck. However, what I have noticed is that the last line that tells enemy object to move doesn't work even thought it is fired. I didn't have this problem before while working on this project for a half a year. This is the Code for courotine:

 IEnumerator FallowPath()
     {
         
         if (path.Length <1)
         {
             StopCoroutine("FallowPath");
         }
         else {
             Vector2 currentWaypoint = path[0];
 
             targetIndex = 0;
 
             while (true)
             {
                 if (transform.position.Equals(currentWaypoint))
                 {
                     targetIndex++;
 
                     if (targetIndex >= path.Length)
                     {
                         yield break;
                     }
 
                     currentWaypoint = path[targetIndex];
                 }
 
                 transform.position = Vector2.MoveTowards(transform.position, currentWaypoint, speed * Time.deltaTime);
                 
                 yield return null;
             }
         }
     }
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

2 Replies

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

Answer by Extrumus · May 18, 2019 at 01:43 PM

The problem was solved by bumping up linear drag up to 100 or more. A silly problem and even sillier solution. But hey, it works

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
avatar image
0

Answer by Bunny83 · May 18, 2019 at 01:11 PM

Well a common issue with such a setup is that you possibly have two or more coroutines running at the same time which are fighting each other since they work on the same object. Note that you should never use StopCoroutine with a string argument. This only works for coroutines started with a string name. If you just want to stop the current coroutine either use yield break or just let it reach the end of the method which will naturally terminate the coroutine.


Note that transform.position is a Vector3. This is not a particular problem but Equals will compare all 3 values. Your currentWaypoint will be converted into a Vector3 with z being set to 0. Just be aware of possible issues.


To track down the issue I would confirm that you never run more than one coroutine at a time and that no other code is messing with the objects position while the coroutine runs. You can print out Time.frameCount along with your debug.log message. It helps to track down multiple iterations per frame.


Since we don't know when and how you start your coroutine we can't say much more about your issue.

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 Bunny83 · May 18, 2019 at 01:22 PM 0
Share

Another issue could be that you have a rigidbody on the object and you actually get stuck somewhere so you never reach the destination. When you unpause the game you usually get a relatively large deltaTime spike (due to the pause) which would let the coroutine reach the current destination immediately and it moves to the next waypoint.

avatar image Extrumus Bunny83 · May 18, 2019 at 01:24 PM 0
Share

Yes, the object does have a rigidbody, but everything is happening in 2d and there is nothing in the way of the moving object

avatar image Extrumus · May 18, 2019 at 01:22 PM 0
Share

Thank you. Some of the suggestions are worth while. There are no multiple coroutines running as before starting one there is always a command that ends same named one. I would guess that this could be Unity problem, because solving this problem for a bit with a pausing and unpausing feels not very script-problem-ish. Also it runs perfectly if a new request has been made

avatar image Bunny83 Extrumus · May 18, 2019 at 01:30 PM 0
Share

Have you read that bit about StopCoroutine carefully? How do you start your coroutine? like this:

 StartCoroutine("FallowPath");

or like this:

 StartCoroutine(FallowPath());

As i said StopCoroutine would only work with the first case, not with the second. Also StopCoroutine only stops one instance. If there are already multiple running only one of them would be stopped. If you really want to stop a coroutine from the outside it's better to store the generated Coroutine object in a variable and use StopCoroutine on that object.

 Coroutine follow = null;
 
 // start
 follow = StartCoroutine(FallowPath());
 
 // stop
 StopCoroutine(follow);
avatar image Extrumus Bunny83 · May 18, 2019 at 01:36 PM 0
Share

Yes, I did. I start both of those coroutines with string. I probably have found the problem and it is bumping into said rigidbody by the player. Any ideas how I could fix it?

Show more comments

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

167 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

StartCoroutine_Auto can only be called from the main thread. 1 Answer

Make damage with Coroutine. 1 Answer

Unity for OculusGo: Move an object along the z axis by touchpad 0 Answers

Coroutine Moving 1 Answer

How to move a 2D player by the gamer answering questions (events) instead of controlls? 0 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