- Home /
Stop a coroutine on a single gameobject
So in my current project I have a prefab with an IEnumerator MoveUnit, currently if I try to stop that coroutine on a particular unit, it ends up stoping it for ALL units
my start command (for all units)
StartCoroutine ("MoveUnit", parms);
my stop command (when a particular unit is chosen
StopCoroutine ("MoveUnit");
Answer by Jeff-Kesselman · Nov 21, 2014 at 05:28 PM
From the docs:
Stops all coroutines named methodName running on this behaviour.
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html
You need to put each one on its own game object or have a flag you can set to tell the given unit to exit its co-routine.
In general though, this sounds like a bad use for coroutines.
Why don't you have a data structure for your units and a List in a single object that updates them by looping in Update? Then the data structure can simply have a boolean that says whether it should be updated currently or not.
It will be simpler to control AND more efficient.