- Home /
Problems with nested coroutines: while loop freezes
Hi, I have a problem with nested coroutines... I have a cube that contains other cubes instantiated by a class. I want to 1- translate up the scene 2- empty the cube 3- translate down the cube 4- populate the cube 5- return to position 0 of the cube (whit its childs).
The first 4 steps are ok... But when i use a loop inside nested coroutine that wuold move up the scene, it freezes. Below the code;
void OnDoubleClick ()
{
DirBack.curDir = this.name;
StartCoroutine (Show (this.name, 1.0f));
}
IEnumerator Show (string dirName, float showTime)
{
yield return StartCoroutine (Hide (1.0f));
Dirs.Show (dirName); // Populate the scene cube
Files.Show (dirName); // Populate the scene cube
float showElapsedTime = 0.0f;
while (showElapsedTime < showTime) { // While loop that Freezes
showElapsedTime += Time.deltaTime;
scene.transform.position = Vector3.Lerp (scene.transform.position, new Vector3 (0, 500, 0), (showElapsedTime / showTime));
yield return new WaitForEndOfFrame ();
}
}
IEnumerator Hide (float hideTime)
{
float hideElapsedTime = 0.0f;
while (hideElapsedTime < hideTime) {
hideElapsedTime += Time.deltaTime;
scene.transform.position = Vector3.Lerp (scene.transform.position, new Vector3 (0, 500, 0), (hideElapsedTime / hideTime));
yield return new WaitForEndOfFrame ();
}
// Clear Scene and Populate Scene
foreach (Transform child in scene.transform) {
Destroy (child.gameObject);
}
scene.transform.position = Vector3.Lerp (scene.transform.position, new Vector3 (0, -500, 0), 1.0f);
}
In the gameplay i see the wired main cube blocked to y = -500 with child cubes inside. I've tried for hours...what is wrong?
Nothing immediately jumps out at me, unless the behavior of WaitForEndOfFrame() has changed between versions. Try logging showElapsedTime
to make sure that it's increasing, at least.
I've tried logging the showElapsedTime
increment... it stops at the first step (0.016....)
Answer by areFranz · Dec 06, 2013 at 11:18 PM
Nobody can help me? I'm trying this script now:
void OnDoubleClick ()
{
DirBack.curDir = this.name;
StartCoroutine (ChangeScene (this.name));
}
IEnumerator ChangeScene (string dirName)
{
yield return StartCoroutine (Move (new Vector3 (0, 500, 0), 1.0f));
yield return null;
// Clear Scene and Populate Scene
foreach (Transform child in scene.transform) {
Destroy (child.gameObject);
}
scene.transform.position = new Vector3 (0, -500, 0);
//Dirs.Show (dirName); // Populate the scene cube
//Files.Show (dirName); // Populate the scene cube
yield return null;
yield return StartCoroutine (Move (new Vector3 (0, 0, 0), 1.0f));
}
IEnumerator Move (Vector3 position, float time)
{
Vector3 start = scene.transform.position;
Vector3 end = position;
float t = 0;
while(t < 1) {
yield return null;
t += Time.deltaTime / time;
scene.transform.position = Vector3.Lerp(start, end, t);
}
scene.transform.position = end;
}
But i have the sampe problem: the first nested coroutine works but the second breaks at the first step of the while loop. Why??
I created this from your code to simplify things and this works on 4.3.3.
I am interested to know:
Does this sample work in a $$anonymous$$imal test scene with one object in your version of unity.
Using this sample and then replacing the On$$anonymous$$ouseDown with your OnDoubleClick, does it still work (still in test scene)?
Adding this component on the object in question in the actual scene and using OnDoubleClick, does it still work?
Adding in the destruction code between the yields, does it still work?
Adding in the actual moving and transforms, does it still work?
using UnityEngine; using System.Collections;
public class TestCoroutineExecution : $$anonymous$$onoBehaviour {
void On$$anonymous$$ouseDown () { StartCoroutine (ChangeScene (this.name)); } IEnumerator ChangeScene (string dirName) { yield return StartCoroutine ($$anonymous$$ove (new Vector3 (0, 500, 0), 1.0f)); yield return null; yield return null; yield return StartCoroutine ($$anonymous$$ove (new Vector3 (0, 0, 0), 1.0f)); } IEnumerator $$anonymous$$ove (Vector3 position, float time) { Debug.Log ("Start $$anonymous$$ove"); float t = 0; while(t < 1) { yield return null; t += Time.deltaTime / time; Debug.Log("Time:" + t.ToString ()); } Debug.Log ("End $$anonymous$$ove"); } }
Answer by rasoulcarrera · Jan 23, 2014 at 09:34 PM
Try use another function Type your loop in another function and call that from your step 4
MyLoop(){ float t = 0;
while(t < 1) {
yield return null;
t += Time.deltaTime / time;
Debug.Log("Time:" + t.ToString ());
}
}
Your answer
Follow this Question
Related Questions
Lerp in Nested Coroutines doesn't work if I add some functions inside the Coroutines 1 Answer
Coroutine stops after any yield statement halfway through 0 Answers
TBG,If,While 1 Answer
Nested coroutine handling 0 Answers
Error with nested coroutines 0 Answers