- Home /
StopCourutine not working as expectedd
private IEnumerator Move()
{
while (!RunGameScript.Instance.IsGameRunning)
{
yield return null;
}
yield return new WaitForEndOfFrame();
this.SetDirectionBeginning();
while (true)
{
if (RunGameScript.GameOver)
{
StopCoroutine(this.Move());
}
else if (this.HasReachedLimit)
{
this.SetDirection();
}
yield return null;
}
}
The problem is that it enters million times in the GameOver if and I don't want to paste this if in the update. Any idea why this doesn't prevent the move method from invoking? I tried also to use StopCouroutine with string but still didn't work.
Answer by iwaldrop · Dec 31, 2014 at 06:43 AM
As far as I know, this is the easiest way to exit a coroutine.
yield break;
Answer by Thom Denick · Dec 31, 2014 at 06:01 AM
StopCoroutine doesn't work like that. You want to just:
StopCoroutine("Move");
or
// variable that holds reference to coroutine (IEnumerator)
public IEnumerator routine;
// get IEnumerator from Coroutine and start
routine = DoWork();
StartCoroutine( routine );
// stop Coroutine
StopCoroutine( routine );
From here: http://forum.unity3d.com/threads/how-to-use-the-new-stopcoroutine-ienumerator.249732/
http://pastebin.com/ftfxEQSY - is this the way I am supposed to do it in my case?
I hope "StopCourutine" (pastebin) is a copy paste error.
Yeah it is I just wrote in at pastebin itself not in the editor there might be some other mistakes like this, but still is this the way to achieve the expected results?
Your answer
![](https://koobas.hobune.stream/wayback/20220613175243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio mixer Setfloat() method problem in coroutine 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Code with Coroutine not working as intended. 2 Answers
why am i getting this error?(not all code paths return a value) 2 Answers