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....)

what is that broken link?

It was spam/phishing. I've deleted it.

2 Answers

2

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??

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 ());
}

}