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 boddole · Mar 10, 2014 at 06:54 AM · c#coroutineprogrammingloop

Coroutine For Loop Will Not End

Hello everyone, I have been trying to accomplish a screen fade effect using a coroutine, but I can't actually get the For loop condition to ever be met.

In the code below, the script will get very close to "counter >= value" but will never get there. In this case .1, but if I change it to say .3 instead, then the script will get very close to .3 but never reach it.

If anyone has an idea or answer why this is happening, I would love to know.

     IEnumerator FadeToClear (float time) //after load is done, wait then fade into game (screen -> clear), 
     {
         //yield return new WaitForSeconds (time);
 
         for (float counter = 1; counter >= 0.1f; counter -= fadeSpeed * Time.deltaTime)
         {
             Debug.Log ("counter is " + counter);
             //Debug.Log ("reducing panel opacity " + panelLoadingScreen.alpha);
             panelLoadingScreen.alpha = counter;
             yield return null;
         }
 
         if (panelLoadingScreen.alpha <= 0.1f)
         {
             Debug.Log ("turning off loading screen");
             cameraLoadingScreen.SetActive(false);
         }
     }
Comment
Add comment · Show 8
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 zee_ola05 · Mar 10, 2014 at 07:19 AM 1
Share

The code looks ok to me. Do you change the value of fadeSpeed in other parts of your code?

Have you tried changing fadeSpeed * Time.deltaTime to a constant just to test?

avatar image boddole · Mar 10, 2014 at 07:43 AM 0
Share

@ zee_ola05: Indeed I have, I used to have it just be "counter -= 0.1f", same problem. In fact, when watched closely, the value approaches the condition, gets close to it, then starts to increase, then decrease, then increase, etc...very strange.

avatar image fafase · Mar 10, 2014 at 07:43 AM 0
Share

Are you doing something with fadeSpeed somewhere else?

avatar image boddole · Mar 10, 2014 at 07:54 AM 0
Share

@ zee_ola05: -not doing anything else with "fadeSpeed". -It could be something with update, it is called at the end of this script: (as I think about it, I used to have a line of code that disabled the script after the GO was not null so it only ran 1 time...I suppose that since that line is gone, since the coroutine is still running then Update is still Updating...

     void Update ()
     {
         //Debug.Log ("main menu script is updating");
         worldRoot = GameObject.FindGameObjectWithTag("WorldRoot");
         if (worldRoot != null) //does not count additive levels as true loads
         {
             panelLoadingScreen = GameObject.Find ("ContainerLoadingScreen").GetComponent<UIWidget>();
             cameraInGameUI.SetActive (true);
             loadingScreenCamera.SetActive (false);
 
             //camera$$anonymous$$ovement = GameObject.Find("0$$anonymous$$ainCamera").GetComponent<Camera$$anonymous$$ovement>();
             //camera$$anonymous$$ovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
 
             player$$anonymous$$ovement = GameObject.Find("PlayerPrefab").GetComponent<Player$$anonymous$$ovement>();
             player$$anonymous$$ovement.leftVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickLeft").GetComponent<VirtualAnalogStick>();
             
             camera$$anonymous$$ovement = GameObject.Find("0$$anonymous$$ainCamera").GetComponent<Camera$$anonymous$$ovement>();
             camera$$anonymous$$ovement.rightVirtualAnalogStick = GameObject.Find("SpriteVirtualAnalogStickRight").GetComponent<VirtualAnalogStick>();
 
             if (useSavedDataToggle.loadSavedData == true)
             {
                 //loadSavedGame.enabled = true; //turn the script back on, 
                 //loadSavedGame.shouldLoadSavedData = true; //load the saved data, 
             }
 
             //this.enabled = false;
             StartCoroutine (FadeToClear(waitToFadeIn)); // new way of fading into game 3 8 14, 
         }
     }
avatar image whydoidoit · Mar 10, 2014 at 08:06 AM 1
Share

You are starting the coroutine every frame - you only want to do that once.

Show more comments

1 Reply

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

Answer by fafase · Mar 10, 2014 at 07:47 AM

Are you making sure your coroutine is called only once? I mean it could be that you are triggering it many times in the Update and they just pile and what you see (values up and down) is just the result of many different coroutines doing their jobs.

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

23 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Trouble with Iterative Coroutine 1 Answer

Problem with getting a value from a enum 2 Answers

How to loop a video texture in C# 1 Answer

Instantiating Prefabs through Editor Script 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