Custom Coroutine With Arguments
Hey was wandering is it possible to start a coroutine which name s stored in a variable and have it also pass arguments to the coroutine? Example
public void ExampleVoid(string coroutineName,int coroutineIntOne,int coroutineIntTwo){
//here i want to call the coroutine by the string name which works fine
StartCoroutine(coroutineName);
//but what i want to do is also pass the ints to the coroutine but i have tried it a few ways and searched for help but nothing came up.
//in all honesty i thought this would have been acceptable
StartCoroutine(coroutineName(coroutineIntOne,coroutineIntTwo));
//but it isn't acceptable and i can't seem to figure any way to do it
}
any insight on this matter would be greatly appreciated.
The general feeling is to avoid calling a coroutine using a string. It's sort of a starter-trick. I think the idea is, once you're doing something as complicated as this, may as well call it using the real variable (which should work fine using your 2nd approach.)
ID$$anonymous$$ about the "general feeling" but as the docs say...
In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name.The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.
Right - that first sentence says to avoid the string method if you can; and the rest is obsolete. You can now store a reference to any running coroutine, which can be used to stop it. That's better since you can have 3 copies of the same coroutine running, and stop just one (stopCoroutine on a string stops them all.)
But, it takes program$$anonymous$$g experience to get used to variables that can be functions; which takes time. If, say, you're an artist with just enough scripting to get the job done, the string method is fine.
Answer by b1gry4n · Oct 19, 2016 at 03:13 AM
http://answers.unity3d.com/questions/240959/pass-more-than-one-parameter-for-startcoroutine.html
A work around in your case could be to store the variables outside the coroutine.
int coOne;
int coTwo;
public void ExampleVoid(string coroutineName,int coroutineIntOne,int coroutineIntTwo){
coOne = coroutineIntOne;
coTwo = coroutineIntTwo;
StartCoroutine(coroutineName);
}
and then inside your coroutine, you could just reference "coOne" and "coTwo"
Another solution is to create a struct or class.
public class CoVals{
public int val1;
public int val2;
public CoVals(int val1, int val2){
this.val1 = val1;
this.val2 = val2;
}
}
And since only one object can be passed using the string method...
StartCoroutine(coroutineName, new CoVals(coroutineIntOne, coroutineIntTwo));
IEnumerator WhateverCoroutine(CoVals covals){
int one = covals.val1;
int two = covals.val2;
}
https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
Great answer. Adding to this, you could also pass an object array:
int intOne = 56;
int intTwo = 10;
object[] parms = new object[]{intOne,intTwo};
StartCoroutine("$$anonymous$$yCoroutine", parms);
public IEnumerator $$anonymous$$yCoroutine(object[] parms)
{
int coroutineIntOne = (int)parms[0];
int coroutineIntTwo = (int)parms[1];
//Do something
yield return null;
}
thanks for the answer i have no idea why it never crossed my $$anonymous$$d to store it and reference it
Your answer
Follow this Question
Related Questions
c# shooting problem (probably pretty simple) 2 Answers
StartCoroutine not listening to parameters 1 Answer
Stop previous coroutine and start new one from beginning 1 Answer
Weird problem with simple boolean and "if" statement [C#] 0 Answers
Waiting for keypress from user (how to make a co-routine?) C# 2 Answers