I wonder why does Unity NOT support an overload function of “StopCoroutine()” as “StopCoroutine( Coroutine toStop )” ?
It seems some kind of instincts to have such function, but… it never appear in Unity.
The coroutine are using everywhere in our C# scripts, we use a self-made coroutine system for simulating “thread” to control objects with time line.
Fore example, if we want to control an object to change color and move in the same time, we will do it like this:
//TPCode is a class to track Time.deltaTime and calculate time interval for any action.
IEnumerator Thread_Color( TPCode t )
{
//Change color to white in 0.5 second, and to black in 0.5 second
while( true )
{
yield return t.SetColor( Color.White, 0.5f );
yield return t.SetColor( Color.Black, 0.5f );
}
}
IEnumerator Thread_Move( TPCode t )
{
//Move position to (0, 0, 0) in 0.5 second, and to ( 100, 100, 100 ) in 0.5 second
while( true )
{
yield return t.MoveTo( Vector3.zero, 0.5f );
yield return t.MoveTo( new Vector3( 100.0f, 100.0f, 100.0f ) , 0.5f );
}
}
void Start()
{
//NewThread() will new a TPCode object and call StartCoroutine() to execute "Thread"
NewThread( Thread_Color );
NewThread( Thread_Move );
}
I’ve migrated this “Thread - Time simulating system” from my C++ old codes, and it is very convience for designing game for us. Our scripts’ code will be simple and easy to control object and maintain modification.
But It caused great inconvenience without this function “StopCoroutine( Coroutine toStop )”.
I wonder…is there any Update plan of Unity will support this function type of “StopCoroutine( Coroutine toStop )”?
StopCoroutine(“myFunction”) is a solution to the problem, but I’ve experienced another problem recently : StopCoroutine doesn’t stop the coroutine 100% of the time.
Seems more likely that there’s a logic error somewhere, as I use coroutines all the time and in my experience they are 100% solid (including StopCoroutine).
Since I’ve built a system to control all GameObject, even our home made TPSpriteManager2 to control all sprites, I won’t have ONLY ONE coroutine in the same name. Such as “IEnumerator inMove( Vector3 pos )” could be used for more than 1 time.
If I stop the coroutine by the string parameter version, all the coroutine named “inMove” will be stopped.
I need to control each coroutine in a very precise way.
if you need to stop a specific one, you can’t use StopCoroutine. You will need a condition that the coroutine itself checks and upon which it terminates.
if it is otherwise ongoing you likely have a toplevel while loop in there, so just use this termination condition as while condition for example
In that case I’m not sure why you’re asking for any kind of StopCoroutine overload, because it would still have the same problem. However, if you have multiple instances of a script running, StopCoroutine(“X”) will stop only the coroutine running in that particular instance. If you have multiple instances of a coroutine running in the same script, you’d have to do what dreamora said.
What could be a nice feature is something like:
var myRoutine = StartCoroutine(SomeFunction());
…
myRoutine.Stop();
StartCoroutine is just an enhanced wrapping around the .NET iterators as far as I can say and they wouldn’t return any kind of handle to work with and return. I would assume that the name based binding is already the consequence of some lookup structure causing overheads due to the required management.
That could naturally be missused for specific coroutine termination, but cause even more overhead …
all in all it would be better anyway if unity just was threadsafe or at least not a threading archenemy