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

People who like this

0 Show 0
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
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
Ermiq
bpaynom

People who like this

2 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 instead:

 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 determined 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 overwhelming 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Climbing a block in 2d 0 Answers

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

I want to create a leaderboard/HighScoreboard in unity 4.5 using microsoft azure online server ? 0 Answers

How to start a Coroutine in another script? 1 Answer

Rotating cube steadily around world axis not working properly 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