Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by areFranz · Dec 06, 2013 at 04:33 AM · coroutineswhilenested

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Dec 06, 2013 at 04:47 AM 0
Share

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.

avatar image areFranz · Dec 06, 2013 at 01:43 PM 0
Share

I've tried logging the showElapsedTime increment... it stops at the first step (0.016....)

avatar image areFranz · Dec 06, 2013 at 02:19 PM 0
Share

what is that broken link?

avatar image Loius · Dec 06, 2013 at 04:12 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GravelFrog · Jan 23, 2014 at 06:37 PM 0
Share

I created this from your code to simplify things and this works on 4.3.3.

I am interested to know:

  1. Does this sample work in a $$anonymous$$imal test scene with one object in your version of unity.

  2. Using this sample and then replacing the On$$anonymous$$ouseDown with your OnDoubleClick, does it still work (still in test scene)?

  3. Adding this component on the object in question in the actual scene and using OnDoubleClick, does it still work?

  4. Adding in the destruction code between the yields, does it still work?

  5. 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");
         }
     }
     
    
avatar image
0

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges