How to add coroutines with an existing argument To a dictionary?
some thing like this doesn't work
public Dictionary<string, Func<Vector3, IEnumerator>> URDD;
private void Start()
{
URDD = new Dictionary<string, Func<Vector3, IEnumerator>>() {
["U"] = movePlayerUpCDirectionlist(Vector3.right)
};
}
and neither this
Dictionary<string, Coroutine> UpD = new Dictionary<string, Coroutine>();
Coroutine crt = StartCoroutine(movePlayerRightC(Vector3.right));
Answer by Hellium · Jul 28, 2021 at 04:05 PM
It should be either:
private void Start()
{
URDD = new Dictionary<string, Func<Vector3, IEnumerator>>() {
["U"] = movePlayerUpCDirectionlist
};
}
And call StartCoroutine(URDD["U"](Vector3.right));
or
private void Start()
{
URDD = new Dictionary<string, IEnumerator>() {
["U"] = movePlayerUpCDirectionlist(Vector3.right)
};
}
And call StartCoroutine(URDD["U"]);
it works without argument But it doesn't work with argument and I need with
I've just tried the two snippets of code and they both work.
You haven't provided the definition of movePlayerUpCDirectionlist
and not even explained what "does not work", so it's impossible to help you further.
there is an error here ["U"] = movePlayerUpCDirectionlist(Vector3.right)
when I pass an argument And I saw the logic of the error Im passing just an argument but the dictionary's second parameter is a Function but never $$anonymous$$d I will just create an separate array of string And then will get from there a random index and with the help of "IF ELSE" will match index with my Coroutines Then just launch Coroutines like usual with an argument
I didn't put the declaration of the dictionary in the 2nd code snippet but obviously, the type of URDD
should be Dictionary<string, IEnumerator>
Your answer
Follow this Question
Related Questions
How to force IEnumerator methods to be called only with StartCoroutine 2 Answers
Is there a way to check something every frame from within a coroutine? 1 Answer
Calling coroutine in a method issue. 1 Answer
Can WaitForSeconds run multiple times per frame? 1 Answer
Checking Internet inside a Coroutine 0 Answers