Coroutine help

Hi!

I have started to use coroutines and they’re amazing! :slight_smile: But I can’t seem to stop them using the StopCoroutine method.

I’m sure I’m doing something wrong with the string I have to use as name of the coroutine, because StopAllCoroutines works perfectly.

My coroutine is named

IEnumerator smoothMove()

And if i use

StopCoroutine(“smoothMove”)

then it doesn’t work(I’m sure of it because it spams the Debug.Log I’ve putted in, while with StopAllCoroutines() it stops)

Thx :smile:

I have not used coroutines within unity yet however the API does have a comment.
Note: Do not mix the two arguments. If a string is used as the argument in StartCoroutine, use the string in StopCoroutine. Similarly, use the IEnumerator in both StartCoroutine and StopCoroutine.

Can you post the code you use to start the coroutine.

1 Like

Of course :slight_smile:
I use

public void MoveUnit(Stack path, Tile finaltile)
{
StartCoroutine(smoothMove(path, arrival));
}

So you mean I should put a string as an argument like this

public void MoveUnit(Stack path, Tile finaltile)
{
StartCoroutine(smoothMove(path, arrival,“STRING”));
}

and then use the string to stop the Coroutine?
Seems strange to me, to be honest.

In order to StopCoroutine(“smoothMove”) work, you need to start the coroutine with the same method StartCoroutine(“smoothMove”, parametters)

Edit:

And if you want to use more than 1 parameter you need to save the coroutine into a variable:

IEnumerator coroutine = smoothMove(path, arrival);

And now you can call StartCoroutine(coroutine); or StopCoroutine(coroutine);

1 Like

Thank you mate, :slight_smile: but now there is another issue

I’ve tried to do as you said.

In a nutshell, I do like this(I’ve removed some non-pertinent lines)

public void MoveUnit(Stack path, Tile arrival)
{

IEnumerator SmoothMove = smoothMove(path, arrival); //The variable has capital S

StartCoroutine(SmoothMove);

}

then from inside the coroutine

IEnumerator smoothMove(Stack path, Tile arrival)
{
StopCoroutine(SmoothMove); //I’ve also tried with “SmoothMove”
}

but it gives me this error message on the SmoothMove inside StopCoroutine:

the name “SmoothMove does not exist in the current context”

Why?

It is because you need to declare SmoothMove outside the IEnumerator (Edit: I mean outside the MoveUnit function):

// declared outside the coroutine
private IEnumerator SmoothMove;

public void MoveUnit(Stack path, Tile arrival)
{

// set the coroutine
SmoothMove = smoothMove(path, arrival); //The variable has capital S

StartCoroutine(SmoothMove);

}

then from inside the coroutine

IEnumerator smoothMove(Stack path, Tile arrival)
{
// and now you can acces the variable since it exist in the current context
StopCoroutine(SmoothMove); //I’ve also tried with “SmoothMove”
}

You cannot acces variables that has not been declared in the current context.

1 Like

Awesome now it works as it should, and, more important, I’ve learned something really useful. :slight_smile:

Thanks a lot! :smile:

You are welcome!

1 Like