- Home /
Can I use StartCoroutine(CoroutineName()) instead of StartCoroutine("CoroutineName") from another script?
So what's going on is that I have a script with the below Coroutine:
public IEnumerator ThingHandler(float input, bool active)
{
yield break;
}
And I'm trying to call it from another script using StartCoroutine, but because I have 2 parameters I can't use
StartCoroutine("CoroutineName");
and instead I'm forced to use
StartCoroutine(ThingHandler(2, true));
as the first method does not allow me to pass in 2 parameters.
Using
GetComponent<FirstScript>().StartCoroutine ...
from another script does not allow me to use the non-string method of calling the coroutine as it returns an error:
The name 'ThingHandler' does not exist in the current context.
I'm not sure what I can do at the moment, should I be adding a new function under the first script and start the coroutine from there instead? Or is there a more elegant way of solving the issue? Am I doing anything obscenely wrong?
Answer by CorruptedTNC · Jul 30, 2017 at 11:59 AM
I had to make an assumption here, which was that both your scripts inherit from Monobehaviour and are attached to a GameObject.
I think you're getting confused about the context that StartCoroutine should be used in. StartCoroutine is just a method which you can call and supply it with the coroutine you want to use.
You were on the right track with your attempt, pointing your 2nd script towards the first, but StartCoroutine should be the first thing you call.
public class CoroutineContainer : MonoBehaviour
{
public IEnumerator ThingHandler(float input, bool active)
{
Debug.Log($"You called me with {input}, and {active}");
yield break;
}
}
I've included a Debug.Log line in a fake script containing the coroutine, so that you can verify this works if you want to play around some more.
public class Caller : MonoBehaviour
{
public CoroutineContainer coroutineContainer;
private void Awake()
{
StartCoroutine(coroutineContainer.ThingHandler(5.0f, true));
}
}
The reference to CoroutineContainer is more than enough to start off our coroutine. When putting Caller on a gameobject, you'll need to specify which GameObject in your scene contains the CoroutineHandler in the inspector.
Another alternative for the caller is to have the following:
public class Caller : MonoBehaviour
{
public Transform coroutineObject;
private void Awake()
{
var coroutineContainer = coroutineObject.GetComponent<CoroutineContainer>();
StartCoroutine(coroutineContainer.ThingHandler(5.0f, true));
}
}
This option tends to be more useful if objects are likely to disappear or be changed frequently, so the reference that you add manually won't be valid after some time.
Perfect. I simply confused myself by comparing StartCoroutine to directly calling a function in the other script.
Thank you!
Your answer
Follow this Question
Related Questions
How can I spawn different GameObjects using IEnumerator? 1 Answer
How do I pass a parameter for trigger animations in the animator controller unity 5 file 0 Answers
How to make animation state follows float paramets? 0 Answers
How can I access Animator parameters in C# 2 Answers
Serializable class with coroutines? 1 Answer