Answer the question
In order to leave comments, you need to log in
Why is this code not working?
Hello!
There is this piece of code:
void ChangePos(List<Vector2> pointss, GameObject temp)
{
for (int i=0; i<pointss.Count; i++)
{
StartCoroutine(ExecuteAfterTime());
temp.transform.position = pointss[i];
}
}
IEnumerator ExecuteAfterTime()
{
yield return new WaitForSeconds(1);
}
Answer the question
In order to leave comments, you need to log in
I'll try to explain it in a simpler way or something))
So you launched the coroutine in the line
StartCoroutine(ExecuteAfterTime());
it will immediately start executing it, and will execute all calls inside the coroutine until it hits some kind of "wait", in your case
yield return new WaitForSeconds(1);
stumbled -> immediately returned to the main thread and continued to execute
temp.transform.position = pointss[i]; and then the cycle.
and conditionally after "wait", it will continue to execute the coroutine (again from the main thread, looking into it)
(in reality, of course, there is a little differently, but it will do for understanding)
void ChangePos(List<Vector2> pointss, GameObject temp)
{
for (int i=0; i<pointss.Count; i++)
{
StartCoroutine(ExecuteAfterTime());
Debug.Log("from FOR _"+i.ToString());
temp.transform.position = pointss[i];
}
}
IEnumerator ExecuteAfterTime()
{
yield return new WaitForSeconds(1);
Debug.Log("Arter 1 second");
}
Because your code is wrong. It is necessary to shove everything into a coroutine.
Because the docs say:
A StartCoroutine function terminates immediately, however, the Coroutine it creates runs as expected.
That is, why the hell is this call not synchronous, the operator waiting for a second ends up in another thread, and the caller continues without any pauses.
And so that you should write like this:
yield return StartCoroutine(.... etc.
Or instead of calling a coroutine, write
yield return new WaitForSeconds(1);
This is how it will work:
void ChangePos(List<Vector2> pointss, GameObject temp)
{
for (int i=0; i<pointss.Count; i++)
{
StartCoroutine(ExecuteAfterTime(1, i));
}
}
IEnumerator ExecuteAfterTime(int second, int incr)
{
yield return new WaitForSeconds(second);
temp.transform.position = pointss[incr];
}
Yes, shove the cycle into the coroutine and call it once, why do you fence such constructions?
In general, in the latest versions it has long been possible to do this:
async Task ChangePos(List<Vector2> pointss, GameObject temp)
{
for (int i = 0; i < pointss.Count; i++)
{
temp.transform.position = pointss[incr];
await Task.Delay(1000);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question