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 Rabbit · Nov 02, 2013 at 04:23 AM · coroutineloopwhile-loop

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();
Comment
Add comment · Show 6
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 highpockets · Nov 02, 2013 at 07:25 AM 0
Share

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.

avatar image Rabbit · Nov 02, 2013 at 08:11 AM 0
Share

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.

avatar image Bunny83 · Nov 02, 2013 at 09:39 AM 0
Share

If this is your whole function, where do you call "DoSomething" ?

avatar image Rabbit · Nov 02, 2013 at 01:35 PM 0
Share

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.

avatar image Freaking-Pingo · Nov 02, 2013 at 04:21 PM 0
Share

Okay, I got a little checklist:

  1. what about your variable time? What is the value of the variable when it enters the while loop?

  2. Are you perhaps calling LerpScale(float time) by mistake multiple times?

  3. Try test which of the two methods are called first SpawnFruit() or LerpScale()

  4. Are you by any chance calling SpawnFruit() somewhere else?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

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 Rabbit · Nov 02, 2013 at 01:00 PM 0
Share
 yield return null;

I for some reason after reading the docs thought null was 0, anyways thankyou but thats not the issue.

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

18 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

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


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