Why store Coroutine in a variable to make StopCoroutine work?

I wonder why simply StopCoroutine(CoRoutineName) does not work while calling StartCoroutine(CoRoutineName) works? I see that putting it into a Coroutine type variable solves it, but why exaclty? The StartCoroutine is called only once I suppose, since the if (Input.GetButtonDown) could be only true or false, and thus, I suppose it only starts the CoRoutineName coroutine once. And hence, simply stopping should be as simple as starting it, right? Code below just for clarity:

If my question is not really clear, please let me know. :slight_smile:

1 Like

Hi! Welcome to the community!

Look at coroutines as lists or arrays (They are even under the same namespace: System.Collections), if you want to access a particular value of an array you can’t simply say; give me the value, it doesn’t matter if you only have one variable in there, you have to specify, same goes for coroutines, if you don’t specify, the code won’t know which coroutine to stop, even if there’s only one iteration running at that particular time.

If you want to know why exactly it works the way it does, well, that’s quite the rabbit hole, but I suppose you can start by looking into this:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms