Skip to main content
Clarifying some limitations with this strategy
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Is there a way to do this without StartCoroutine()?

Sure, you can step the IEnumerator manually.

var nestedRoutine = JumpToTarget(target);
while(nestedRoutine.MoveNext())
    yield return null; // Wait a frame and check again next time.
                       // Note that this ignores what nestedRoutine yielded!
                       // If it's waiting on a WWW to download, and we don't
                       // handle that case ourselves, we'll pump it too early.

// Execution resumes here once JumpToTarget finishes all its work.

But this seems like substantially more work than just

yield return StartCoroutine(JumpToTarget(target));
// Execution resumes here once JumpToTarget finishes all its work.
// This correctly handles all yield return types that Unity accepts,
// since we're letting the Unity engine handle the yielding logic.

Is there a way to do this without StartCoroutine()?

Sure, you can step the IEnumerator manually.

var nestedRoutine = JumpToTarget(target);
while(nestedRoutine.MoveNext())
    yield return null;

// Execution resumes here once JumpToTarget finishes all its work.

But this seems like substantially more work than just

yield return StartCoroutine(JumpToTarget(target));
// Execution resumes here once JumpToTarget finishes all its work.

Is there a way to do this without StartCoroutine()?

Sure, you can step the IEnumerator manually.

var nestedRoutine = JumpToTarget(target);
while(nestedRoutine.MoveNext())
    yield return null; // Wait a frame and check again next time.
                       // Note that this ignores what nestedRoutine yielded!
                       // If it's waiting on a WWW to download, and we don't
                       // handle that case ourselves, we'll pump it too early.

// Execution resumes here once JumpToTarget finishes all its work.

But this seems like substantially more work than just

yield return StartCoroutine(JumpToTarget(target));
// Execution resumes here once JumpToTarget finishes all its work.
// This correctly handles all yield return types that Unity accepts,
// since we're letting the Unity engine handle the yielding logic.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Is there a way to do this without StartCoroutine()?

Sure, you can step the IEnumerator manually.

var nestedRoutine = JumpToTarget(target);
while(nestedRoutine.MoveNext())
    yield return null;

// Execution resumes here once JumpToTarget finishes all its work.

But this seems like substantially more work than just

yield return StartCoroutine(JumpToTarget(target));
// Execution resumes here once JumpToTarget finishes all its work.