I am trying to call a function after all the coroutines called in a function have finished
void f()
{
Debug.Log("First");
Queue<Vector2> vectors = new Queue<Vector2>();
vectors.enqueue(Vector2.up);
vectors.enqueue(Vector2.right);
vectors.enqueue(-Vector2.up);
vectors.enqueue(-Vector2.right);
foreach(Vector2 v in vectors){
StartCoroutine(g(v));
}
}
IEnumerator g(Vector2 v)
{
Debug.Log(v);
// This is just an example. Not what I am really doing
yield return new WaitForSeconds(2*v.x*v.x+3*v.y*v.y);
h()
}
void h()
{
Debug.Log("Last");
}
The console will result in this
First
(0.0,1.0)
(1.0,0.0)
(0.0,-1.0)
(-1.0,0.0)
Last
Last
Last
Last
How can I set up my code to not call the last function multiple times?
I have also tried this:
void f()
{
StartCoroutine(g(vectors));
}
IEnumerator g(Queue<Vector2> vectors)
{
foreach(Vector2 v in vectors){
yield return new WaitForSeconds(t);
}
h()
}
This ends up doing each one consecutively instead of simultaneously.