Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 iMakeMehGames · Jan 05 at 02:52 AM · coroutinecrashes

Co-routine with loops crashes Unity

(ANSWERED) I have a script that moves an object from one position to another. I made this script so the motion would be smooth, but all it does is freezes Unity. The code:

 // Context Libraries
 using UnityEngine.UI;
 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 using TMPro;
 
 // Context Variables
 public Transform basePos;
 public Transform aimPoint;
 public float aimSpeed;
 private bool canAimChange = true;
 
 // Issue Code
 private IEnumerator AimMove(string type) {
         switch(type) {
             case("to"):
                 canAimChange = false;
                 while(transform.position != aimPoint.position) {
                 Vector3.MoveTowards(basePos.position, aimPoint.position, aimSpeed * Time.deltaTime);
                 }
                 canAimChange = true;
             break;
             case("fro"):
                 canAimChange = false;
                 while(transform.position != basePos.position) {
                 Vector3.MoveTowards(aimPoint.position, basePos.position, aimSpeed * Time.deltaTime);
                 }
                 canAimChange = true;
             break;
         } 
         yield return null;
     }

What is the issue with the code? Am I using the wrong method to smoothly move an object from one point to another? (preferably without using update)

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

3 Replies

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

Answer by Eno-Khaon · Jan 05 at 03:35 AM

You're getting yourself stuck in an infinite loop in a fairly clear way:

 while(transform.position != aimPoint.position)
 {
     Vector3.MoveTowards(basePos.position, aimPoint.position, aimSpeed * Time.deltaTime);
 }
 yield return null;


Ignoring for now that your condition itself isn't great (testing for exact equality of float-based values isn't guaranteed to be reliable - You should test whether the square magnitude of the relative vector is within a small threshold), the key element is the placement of your yield statement, combined with values that won't update (transform.position relative to basePos.position) in your loop.

At best, it might've finished the loop instantaneously. Instead, you lock up in an infinite loop.

Because the yield statement is what controls your looping process, there are numerous ways you can solve this problem, so here's an example of one which makes minimal changes to what you have already:

 // Example threshold
 float distanceThreshold = 0.0001f; // 0.01 magnitude squared
 // ...
 while((transform.position - aimPoint.position).sqrMagnitude < distanceThreshold)
 {
     Vector3.MoveTowards(basePos.position, aimPoint.position, aimSpeed * Time.deltaTime);
     // Inside the loop, so it will wait for the next frame
     // DURING the loop, rather than AFTER the loop
     yield return null;
 }
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
2

Answer by Aluminum18 · Jan 05 at 03:30 AM

Your program stucks in while loop. Move this yield return null; into these two loops.

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 iMakeMehGames · Jan 05 at 03:22 PM

@Aluminum18 @Eno-Khaon Wouldn't that instantly stop the loop with the object only a little closer to it?

Never mind, thank you!

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

144 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

Related Questions

Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers

How to start a coroutine from another script? 1 Answer

How do I do something after multiple coroutines finish? 1 Answer

How to make loop with call to IEnumerator actually pause? 2 Answers

Coroutines Bug? Unity 4.5 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