- Home /
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)
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;
}
Answer by Aluminum18 · Jan 05 at 03:30 AM
Your program stucks in while loop. Move this yield return null;
into these two loops.
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!
Your answer
Follow this Question
Related Questions
Coroutine freezes editor but only sometimes. Why ? Why is it happening randomly ? 3 Answers
How to start a coroutine from Update. 4 Answers
Coroutine does not seem to work 1 Answer
Coroutine couldn't be started becuase inactive 0 Answers
coroutine spamming webserver which then stops replying 2 Answers