What object do coroutines attach themselves to?

I assumed that if an object had a script which implemented a coroutine, that coroutine would be attached to that Behaviour no matter where it was called from. I now know that isn’t the case.

What is the rule for coroutine attachment? That is, for a given coroutine, where do I call StopAllCoroutines() to stop it? How is this affected by stacking and chaining?

Only the script that starts a coroutine can end it. if you need to stop a coroutine from a different script (on the same GameObject or a different GameObject), you can make a reference to the script itself by using the script name.

//assign this in the Inspector
myBehavior : MyBehavior;
myBehavior.StopCoroutine("CoroutineName");

If you can give us a bit more detail about what exactly you’re trying to do, we might be able to assist you better. You can also find more information in the scripting reference docs:

I’m just wondering what defines a script as having started a coroutine. Obviously every function call in Unity can be traced to a callback which is associated with a script. Is it the script whose callback is at the bottom of the stack that owns any coroutines started in the stack?

And what happens in this situation, assuming A, B and C are on three different objects:

Where A spins off b.routine(), which yields for a bit and then yields c.routine(), is c.routine() owned by A or by B? Is the answer different if A yields? If B doesn’t yield? Is there a way to get c.routine() to be owned by C, but still have it initiated by B? And if so, is there a way for this to happen and still have B yield to c.routine()?