Can coroutines only be stopped inside the script that started them? Even if they are both attached to the same game object?
For example, attach both of these scripts to the same GO and the code below will loop endlessly:
public class RunCoroutine : MonoBehaviour {
void Start()
{
StartCoroutine("Test");
}
IEnumerator Test()
{
int i=0;
while (true)
{
i++;
print("Running loop " + i);
yield return null;
}
}
}
public class EndCoroutine : MonoBehaviour {
void Update()
{
if (Time.time > 3f)
{
StopAllCoroutines();
}
}
}
But moving StopAllCoroutines into the first script will work as expected.
public class RunCoroutine : MonoBehaviour {
void Start()
{
StartCoroutine("Test");
}
void Update()
{
if (Time.time > 3f)
{
StopAllCoroutines();
}
}
IEnumerator Test()
{
int i=0;
while (true)
{
i++;
print("Running loop " + i);
yield return null;
}
}
}