- Home /
Coroutine not waiting for While Loop to finish
I am wanting a coroutine to finish a while loop then call DoSomething(), but DoSomething() is getting called before its finished.
while (time > 0.0f)
{
time -= Time.deltaTime;
transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
yield return 0;
}
DoSomething();
It's tough to see what the problem is without you showing more of your script. Generally, you shouldn't need a while statement, as update is a while statement in itself. What is the value of the variable time before you enter the while statement? It's tough to tell what you are doing with: time -= Time.deltaTime.. You are assigning time subtracted by the time it took to finish the last frame to the variable of time. I have a feeling that line is your culprit, but it's tough to know without the value of the time variable.
Thanks for you help. time is a float passed in.
IEnumerator LerpScale(float time) {
Vector3 originalScale = transform.localScale;
Vector3 targetScale = originalScale * GrowthScale;
float originalTime = time;
while (time > 0.0f)
{
time -= Time.deltaTime;
transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
yield return 0;
}
}
I am trying to scale object using lerp and when finished run DoSomething();, I am not sure why its running before its finished.
If this is your whole function, where do you call "DoSomething" ?
thats where im stuck, im calling it after the while loop. after the transform scaling is complete i am instantiating an object at a game object tagged spawnpoint at the top of the object getting scaled. this is what i want to call after the object has been scaled.
void SpawnFruit() {
//use
GameObject[] spawnpoints = GameObject.FindGameObjectsWithTag("FruitSpawn");
Transform spawnpoint = spawnpoints[Random.Range(0, spawnpoints.Length)].transform;
//Instantiate(FruitPrefab , spawnpoint.position, spawnpoint.rotation);
Transform Fruit = Instantiate(FruitPrefab , spawnpoint.position, spawnpoint.rotation)as Transform;
}
but it spawns it early and the object keeps scaling.
Okay, I got a little checklist:
what about your variable
time
? What is the value of the variable when it enters the while loop?Are you perhaps calling
LerpScale(float time)
by mistake multiple times?Try test which of the two methods are called first
SpawnFruit()
orLerpScale()
Are you by any chance calling
SpawnFruit()
somewhere else?
Answer by Freaking-Pingo · Nov 02, 2013 at 10:40 AM
First of all, have you made sure your variable time
is not zero or lower upon entering the coroutine? Second, I am not actually sure what yield return 0
will do, but when I use coroutine and uses while loops, I utilize yield return null
instead.
yield return null;
I for some reason after reading the docs thought null was 0, anyways thankyou but thats not the issue.
Your answer
Follow this Question
Related Questions
Iteration of coroutines 1 Answer
Coroutine while(bool) kills Unity 1 Answer
Running multiple coroutines in loops with different time 2 Answers
Android coroutine loop ruins game 1 Answer
How do I activate game objects in array in timed loop? 1 Answer