Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CrPbI3yH · Sep 01, 2018 at 01:54 PM · scripting problemcanvascoroutine

Unity freezing on Waitforsecond

I'm trying to raise Canvas smoothly and Unity crashes on method call. I have : 1. Canvas Gameobject 2. Script thats calling method 3. Script with Method itself:

       IEnumerator Fader(float sec)
         {
             eg_canvas.GetComponent<CanvasGroup>().alpha += 0.05f;
             yield return new WaitForSeconds(sec);
         }
     
         void EG_canvas_start()
         {
             eg_canvas.SetActive(true);
             eg_canvas.GetComponent<CanvasGroup>().alpha = 0;
             while (eg_canvas.GetComponent<CanvasGroup>().alpha <= 1)
             {
                 StartCoroutine(Fader(0.1f));
                 Debug.Log(eg_canvas.GetComponent<CanvasGroup>().alpha.ToString());
            }
        }
 

What I'm doing wrong? Thanks in advance.

Comment
Add comment
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

1 Reply

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

Answer by eses · Sep 01, 2018 at 02:02 PM

Hi @CrPbI3yH

You'd want to run your coroutine only once - now you are running it in your while loop; as long as alpha is less than or equal to 1, you are starting your coroutine.

You should just start your Fader coroutine once, and do something like this in your Fader coroutine:

 IEnumerator Fader(float duration)
 {
     alpha = 0f;
     var cg = eg_canvas.GetComponent<CanvasGroup>();
 
     while (alpha <= 1f)
     {
         alpha += Time.deltaTime / duration;
         cg.alpha = alpha;
         yield return null;
     }
 }
 

 void EG_canvas_start()
 {
     eg_canvas.SetActive(true);
     StartCoroutine(Fader(10f));
 }

Comment
Add comment · Show 13 · 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 CrPbI3yH · Sep 01, 2018 at 02:24 PM 0
Share

sorry, but I don't see pause here. how waitforsecond must be implemented here? something like this? while (eg_canvas.GetComponent().alpha <= 1) { eg_canvas.GetComponent().alpha += Time.deltaTime; new Waitforseconds(sec); } yield return null;

avatar image eses CrPbI3yH · Sep 01, 2018 at 02:27 PM 0
Share

@CrPbI3yH - You will have to explain - at least I don't get what you mean by Pause? Do you mean, you don't want to start fading, then fade from point A to point B - that is what my example will do. You didn't mention a word about pausing.

avatar image CrPbI3yH eses · Sep 01, 2018 at 02:45 PM 0
Share

I meant that i don't see Waitforseconds in your code.

Show more comments
avatar image Ermiq CrPbI3yH · Sep 01, 2018 at 02:36 PM 0
Share

Don't do this:

  while (eg_canvas.GetComponent<CanvasGroup>().alpha <= 1)
          {
              StartCoroutine(Fader(0.1f));
              Debug.Log(eg_canvas.GetComponent<CanvasGroup>().alpha.ToString());
         }

Do this ins$$anonymous$$d:

 StartCoroutine(Fader(0.1f));
 Debug.Log(eg_canvas.GetComponent<CanvasGroup>().alpha.ToString());

When you have started a coroutine, it will do all stuff asynchronously, every time it reaches yield instruction, the coroutine execution is paused and then resumed in next frame/fixedUpdate/amount of seconds deter$$anonymous$$ed by WaitForSeconds(), etc. It's done automatically. The coroutine will resume itself everytime and will do next step in the while/do/foreach or any other loop instruction. There's no need to start the coroutine using another one loop instruction, as you did in while (eg_canvas.GetComponent<CanvasGroup> ().alpha <= 1) { StartCoroutine(Fader(0.1f)); }.
In your code you're starting a hundred of identical coroutines, since every Update() is called every 0.016 seconds when fps is 60 frames, and all of these coroutines are trying to increase the alpha value, that's quiet overwhel$$anonymous$$g for the Unity and for the CPU to do such things. That's why Unity freezes.

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

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

Related Questions

Coroutine doesn't work when called from another method. 3 Answers

Coroutine not working ? possible errors ? 1 Answer

yield return new WaitForSeconds not working 1 Answer

Ingame music goes on but it should stop when you die (DeathCanvas Activate) or win (LevelComplete Canvas Activates) 1 Answer

How to change a large number of booleans as fast as possible without sacrificing framerate? 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