Skip to content

Instantly share code, notes, and snippets.

@20chan
Last active February 17, 2020 09:58
Show Gist options
  • Save 20chan/779009dfae67dd1f74fc66e8d90f289d to your computer and use it in GitHub Desktop.
Save 20chan/779009dfae67dd1f74fc66e8d90f289d to your computer and use it in GitHub Desktop.
Unity StopCoroutine doesn't work at all in nested Coroutine but it is ambiguous
using System.Collections;
using Sirenix.OdinInspector;
using UnityEngine;
public class DamnUnityCoroutine : MonoBehaviour {
private Coroutine coroutine;
[Button(ButtonSizes.Large)]
public void Go() {
coroutine = StartCoroutine(A());
}
[Button(ButtonSizes.Large)]
public void Stop() {
if (coroutine != null) {
StopCoroutine(coroutine);
}
}
private IEnumerator A() {
while (true) {
yield return B();
}
}
public IEnumerator B() {
while (true) {
yield return C();
}
}
private IEnumerator C() {
// if line a is alive: StopCoroutine doesn't work
// if line b is alive: StopCoroutine works
// line `a`
yield return D();
// line `b`
yield return null;
print($"{Time.time}");
}
private IEnumerator D() {
yield return null;
}
}