Hello everyone, I was doing some work with coroutines and got a result I was not expecting. The critical parts of the script are below, but here is a summary:
Desired result:
//script is doing things
//coroutine suspends execution temporarily, shows debug message confirming it finished
//script continues
Actual Result:
//script is doing things
//coroutine is called
//script finishes execution
//debug message from coroutine appears (so it waited, but did not stop the script while it did)
-I realize I could put the rest of the code in the co-routine, but to keep things clean I’d rather just use the co-routine to stop the execution (which I thought it could do). Any ideas are appreciated.
void MovePosition ()
{
if (nextPhase == false && inMotion == false)
{
transform.localPosition = Vector3.MoveTowards (transform.localPosition, endingPosition, movementSpeed * Time.deltaTime);
if (Vector3.Distance(endingPosition, transform.localPosition) < 0.01f)
{
inMotion = true;
if (inMotion == true)
{
StartCoroutine (WaitForRetract(rectractTimer));
nextPhase = true;
Debug.Log ("next pahse is " + nextPhase + " called from 1st block");
inMotion = false;
Debug.Log ("in motion is " + inMotion + " called from 1st block");
}
}
}
IEnumerator WaitForRetract (float time)
{
yield return new WaitForSeconds (time);
Debug.Log ("I finished waiting");
}