I’m calling StartCoroutine in my script called “Main_GUI” in a public function that I am calling from “Main_Script”. I am also calling StopCoroutine in “Main_GUI” in a public function that I am also calling from “Main_Script”. The coroutine is not stopping and I know for sure that the stop function is being called. Does anyone have an ideas what might be causing this?
Just for clarification: coroutines are bound to the game object that starts them and can only be stopped again on the game object that starts them.
so if the start and stop happen on the same object, no problem.
If they don’t you need to implement a public stop function on the object that is called by the other object
Right now I have ClassB calling a public function in ClassA to start a coroutine and when ClassB decides that ClassA shouldn’t finish the coroutine, ClassB calls a public function in ClassA that calls StopCoroutine. But the coroutine doesn’t stop for some reason. Hopefully that makes sense.
Sorry I took so long to respond, my company moved buildings and I’ve been busy at home.
I realize this isn’t the best code, I needed to get a lot of stuff done in a short period of time. I am now in the process of going back and fixing all the stuff that I slapped together before.
Anyways, I call StartGameOverUI after the fireworks for beating a level play and I call StopGameOverUI if the player starts a new level before the scores have finished rolling up and the grade appears.
I know that it’s not stopping because I will start a new level with the printout: “in StopGameOverUI” over a second before the stamp would show up and it still shows up along with the debug statement: “about to stamp”.
Any thoughts anyone has on what the cause of the problem could be would be much appreciated as this project is almost complete and I am running out of other features to add and fix.
One problem I noticed is that when you call StartCoroutine, you are calling it with the function
StartCoroutine(ShowGameOverUI(waitTime)); syntax instead of StartCoroutine(“ShowGameOverUI”, waitTime) syntax.
The documentation says:
function StopCoroutine (methodName : string) : void
Description
Stops all coroutines named methodName running on this behaviour.
Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine.
Necro Post, but google led me here so I wanted to update this since it’s one of the first google results for this problem.
Heisenburg has a great answer over at stackexchange.
You really don’t want to use the other methods (just passing a string or starting coroutines by passing the method & parameters themselves). Instead, you want to keep track of your coroutines so you can have more control to stop them or specific ones.
Calling StopCoroutine(“MoveToPositionOverTime”) did nothing, because I started it with that method above. So now I created a method to do it, like this:
private IEnumerator coroutine_MoveToPositionOverTime; //Only one coroutine should be used at a time.
private void start_UniqueSingularCoroutine_MoveToPositionOverTime(GameObject gob, Vector2 position, float timeToMove)
{
if (coroutine_MoveToPositionOverTime != null)
{
StopCoroutine(coroutine_MoveToPositionOverTime);
}
coroutine_MoveToPositionOverTime = MoveToPositionOverTime(gob, position, timeToMove);
StartCoroutine(coroutine_MoveToPositionOverTime);
}
public IEnumerator MoveToPositionOverTime(GameObject gob, Vector2 position, float timeToMove)
{ }
This works perfectly. I just call this method now:
I have absolutely no idea why this isn’t the standard being taught. I learned the other (more popular) method, despite it being so much worse. I really wish I learned “the right way” the first time. Would have saved me an hour, since I didn’t realize it was the coroutine that was causing my bug until I chased it down & quickly realized StopCoroutine() wasn’t working as I expected.
Thank you so much for your post! I’ve been having such an issue with StopCoroutine() in my current project and your solution solved literally all of my coroutine issues. Why all tutorials teach it the other way is beyond me, because your explanation makes a lot more sense anyway?