- Home /
StopCoroutine. how avoid to stop all my coroutines?
StopCoroutine stops all coroutines in the same behaviour, like it is said in this post and others similar:
Just beware that if the same coroutine is called more than once using this method, then using StopCoroutine will result in all of them being stopped.
I am using the string call method, but it happens the same way using the IEnumerator call method.
Any idea how to stop just 1 coroutine and not all?
Answer by pako · Feb 10, 2018 at 09:03 PM
I've never used a StopCoroutine
statement from inside the coroutine, and haven't seen such usage anywhere. I think this might be the problem.
You should use yield break
instead of StopCoroutine
to stop the coroutine from inside the coroutine.
BTW it would also be a good idea to have the Destroy (planetaTrozosGameObject)
before the yield break
Thanks so much.
It worked. I didn't know the yield break statement usage.
It also worked with the string method coroutine call.
Thanks again.
Actually now i don't use the StopCoroutine statement, so i did't really test any StopCoroutine call method :)
Answer by Hanoble · Feb 10, 2018 at 07:06 PM
The best way is to hold a reference to the IEnumerator and stop just that one, for example.
//IEnumerator object to hold coroutine
private IEnumerator myCoroutine;
//Assign IEnumerator to the coroutine
private void SomeMethod()
{
myCoroutine = WaitAndPrint(3.0f);
StartCoroutine(myCoroutine);
}
//Kill the specific instance of the coroutine set earlier
private void StopCoroutine()
{
StopCoroutine(myCoroutine);
}
Further reading here.
I tried that way, holding a reference to the IEnumerator, but it doesn't work neither.
simplifying the code looks like this:
private IEnumerator coroutine;
void OnTriggerEnter (Collider other)
{
otherParentGameObj = other.transform.parent.gameObject;
if (otherParentGameObj.tag == VariablesScript.TagPlaneta )
{
coroutine = LerpPlanetaHaciaPlayerCoroutine ();
StartCoroutine (coroutine);
}
}
private IEnumerator LerpPlanetaHaciaPlayerCoroutine ()
{
Vector3 planetaPosition = otherParentGameObj.transform.position;
GameObject planetaTrozosGameObject = (GameObject) Instantiate (planetaTrozosPrefab, planetaPosition, Quaternion.identity);
float fraccion = 0f;
float distanciaConsideradaCercana = 1f;
while (true)
{
try
{
if (Vector3.Distance (planetaTrozosGameObject.transform.position, myParentGameObj.transform.position) >= distanciaConsideradaCercana)
{
fraccion += 0.001f;
fraccion = ($$anonymous$$athf.Round (fraccion * 1000)) / 1000;
planetaTrozosGameObject.transform.position = Vector3.Lerp (planetaTrozosGameObject.transform.position, myParentGameObj.transform.position, fraccion);
}
else{
StopCoroutine (coroutine);
Destroy (planetaTrozosGameObject);
}
} catch (Exception e) {
StopCoroutine (coroutine);
Destroy (planetaTrozosGameObject);
}
yield return new WaitForFixedUpdate ();
}
}
In this post the author says that it didn't work for him neither:
If you call multiple coroutines with same name than single StopCoroutine with that name will destroy them all (also started with IEnumerator).