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 Sequence · Mar 30, 2012 at 01:46 AM · guiyield

Script seems to halt if a "yield waitForEndOfFrame" is included

Hey guys,

Basically, I'm scripting a GUI and I was trying to get one GUI to fade out before switching the active script to another GUI. I usually make a while loop like this with a "yield WaitForEndOfFrame" so it doesn't fade out at the speed of light.

  function fadeToOptions ()
 {
     if(GUIBusy)
         return;
 
     GUIBusy = true;
     GUI.color.a = 1.0;
     while(GUI.color.a > 0)
     {
         GUI.color.a -= 0.02;
         print("working" + GUI.color.a);
         yield WaitForEndOfFrame();
     }
     options.enabled = true;
     options.fadeIn();
     this.enabled = false;
     GUIBusy = false;
 }

Basically, if I include that yield in that while loop, the script seems to stop and not do anything. If I take it out it will switch to the other GUI just fine, but it won't really fade out because it goes way to fast without the yield. Anyone know what's going on?

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

2 Replies

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

Answer by Bunny83 · Mar 30, 2012 at 02:33 AM

Well, i never tried a coroutine-while-loop with just WaitForEndOfFrame, but i guess when you reached the end of the current frame and yield again until the EndOfFrame, it will return immediately since the coroutine handler is actually processing the EndOfFrame - stack / queue.

 while(GUI.color.a > 0)
 {
    yield WaitForEndOfFrame();
    GUI.color.a -= 0.02;
    print("working" + GUI.color.a);
    yield; // this waits for the next frame
 }

But i don't quite understand why you need this piece of code at the end of frame?

Setting GUI.color outside OnGUI is pretty much useless. Also usually you aren't allowed to use any GUI stuff outside OnGUI. OnGUI is bound to some global states set by the engine (the Event class for example).

You could use a class variable that is manipulated in the coroutine and used in OnGUI.

Comment
Add comment · Show 2 · 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 Sequence · Mar 30, 2012 at 01:00 PM 0
Share

"You could use a class variable that is manipulated in the coroutine and used in OnGUI."

An excellent idea!

" when you reached the end of the current frame and yield again until the EndOfFrame, it will return immediately since the coroutine handler is actually processing the EndOfFrame - stack / queue."

I'm not exactly sure what you mean by that, could you explain a little more?

avatar image Bunny83 · Mar 30, 2012 at 10:58 PM 0
Share

A coroutine is not a function / method, it's an object. This object has a function which contains your actual code, but the function is ter$$anonymous$$ated at each yield and returns what ever you place behind the yield keyword. WaitForEndOfFrame() will create a new object of WaitForEndOfFrame.

The Unity coroutine scheduler is part of the engine and is responsible for running your coroutine. When you yield the WaitForEndOfFrame object, the scheduler knows that this coroutine should continue at the end of the current frame. When the engine reaches the end of the frame in it's internal loop the scheduler will run all coroutines that has been yielded with WaitForEndOfFrame. Unity stores all this coroutines in a list or queue and will call them one by one. If you yield again with WaitForEndOfFrame it is placed again in this list and will be processed again in the same frame so it has no effect since we are currently at the end of frame.

Here's a great blog about how coroutines work.

avatar image
0

Answer by Eric5h5 · Mar 30, 2012 at 02:01 AM

You can't use yield in OnGUI. Like other Update-esque functions, OnGUI runs every frame and can't be interrupted.

Comment
Add comment · Show 2 · 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 Sequence · Mar 30, 2012 at 02:08 AM 0
Share

Sorry, looks like I needed to reformat my question so it properly separated the code. As you can see, the yield is in it's own function.

avatar image rutter · Mar 30, 2012 at 02:48 AM 0
Share

Are you calling this function from OnGUI()?

(If you're not, it'll error out to begin with, so I imagine the answer is yes.)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is yield not working for me? 2 Answers

yield return null vs yield return WaitForEndOfFrame 4 Answers

Gui label delay 1 Answer

How to make this function disable after 5seconds 1 Answer

How can I run functions in succession, each waiting for the last to complete? 2 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