- Home /
StartCoroutine with string method name from another script?
So I'm trying to thread together coroutines from multiple scripts, which has gone pretty well until hitting a step in which I may need to call StopCoroutine. According to Unity:
"Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine."
Which works fine for me within a single script, e.g. "yield return StartCoroutine("CoMoveChar", moveEnemy);"
My question is.. how the hell do I call a string method name from another script? I've tried:
yield return StartCoroutine("scriptLoc.CoMoveChar", moveBlock);
and
yield return StartCoroutine(scriptLoc("CoMoveChar", moveBlock));
and
yield return StartCoroutine(scriptLoc.("CoMoveChar", moveBlock));
But it doesn't work, or for that matter, even look right. Nor does pre-defining it as a string. Please halp!
The error Unity gives me always looks a bit like this: Coroutine 'StaticVars.selectedChar.GetComponent().CoMoveChar()' couldn't be started!
Answer by Bunny83 · Feb 19, 2013 at 09:38 AM
That's actually not possible. You can't run a coroutine, started with the string version, on another MonoBehaviour instance. What you might want to do is starting the coroutine on the other script instead of running it on your script.
public OtherScript other;
//[...]
yield return other.StartCoroutine("CoMoveChar");
Yeah, I was able to continue my threading by calling a coroutine to call the string coroutine. Got the job done, just not very elegantly. Thanks!