I want to know how to keep “Detect” coroutine running after call another coroutine, called “Move” as in the script below:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public bool test = false;
void Start()
{
StartCoroutine("Detect");
}
IEnumerator Detect()
{
while(true)
{
if(test)
StartCoroutine("Move");
yield return new WaitForEndOfFrame();
}
}
IEnumerator Move()
{
while(true)
{
//Do something
yield return new WaitForEndOfFrame();
}
}
}
The detect co-routine does keep running, it’s the Move coroutine that will behave oddly if test is true. If the test variable is true the co-routine Move will get continually spawned every frame.
I would suggest changing test to false after starting the Move co-routine:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
bool test = false;
void Start()
{
StartCoroutine("Detect");
}
IEnumerator Detect()
{
while(true)
{
if(test)
{
StartCoroutine("Move");
test = false;
}
yield return new WaitForEndOfFrame();
}
}
IEnumerator Move()
{
while(true)
{
//Do something
yield return new WaitForEndOfFrame();
}
}
}
If this is not the behaviour you are seeing please post your actual code and I will edit this answer.