- Home /
StartCoroutine of a child object using String
I want to start a coroutine of another script from a driving script. This works fine in C# like this:
StartCoroutine(mOtherObject.SomeCoroutineFunc());
The problem is that I also need the ability to Stop this coroutine, which only works if the StartCoroutine uses the String form of initialization. So, how do change my StartCoroutine call to work in this case? The following does not work:
StartCoroutine("mOtherObject.SomeCoroutineFunc");
Yes, I know that I could just call mOtherObject.BeginCoroutineFunc()
which in turn would start the coroutine with a String
. But this is not my question. Specifically, I want to know:
Is there a way to use the C# string form of coroutine initialization of another object besides intself?
Answer by Landern · Jun 11, 2014 at 06:25 PM
Perhaps a better idea would be to use SendMessage to start the Coroutine in the accepting components.
Thanks, but I don't understand how that syntax would look. Obviously, this won't work:
mOtherObject.Send$$anonymous$$essage("StartCoroutine(SomeCoroutineFunc)");
How would you set it up?
If you really need to use a string, you could pass the string name of the coroutine as a parameter with your message. It would look like this:
mOtherObject.Send$$anonymous$$essage("Start$$anonymous$$yCoroutine", "SomeCoroutineFunc");
Then inside of your mOtherObject script...
public void Start$$anonymous$$yCoroutine(string coroutineName)
{
StartCoroutine(coroutineName);
}
However, if you're using Unity 4.5, StopCoroutine can now take an enumerator argument. I have not used/tested this, but in theory this would work:
StartCoroutine(mOtherObject.SomeCoroutineFunc());
StopCoroutine(mOtherObject.SomeCoroutineFunc());
Oh nice! I didn't know about Unity 4.5 StopCoroutine now taking an enumerator argument! I'll be updating right now. Thanks! :o)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to Instantiate from correct Icon. 1 Answer
Coroutines randomly stop working 0 Answers