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 martingee · Aug 06, 2011 at 10:53 AM · guicoroutinefade

Fading out GUI Label using coroutine problem

Hi, like many people here i'm new to Unity and scripting in general so forgive my naivety :)

What i'm attempting to do is have a GUI label appear and fade out over time once my player enters a trigger box. Following on from this question and answer:

http://answers.unity3d.com/questions/16756/guitexture-fade-in-out-at-time.html

...i can get it to work fine however it only works the once and it looks like i need to somehow reset the co-routine each time(?) This is the script that controls the GUI element:

 static var Greeting : boolean;
 var color : Color;

 function Start(){
   color = Color.white;
   yield FadeOutAfterTime(3);
 }

 function OnGUI () {

 if (Greeting) {
 GUI.color = color;
 GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello");
 }
 }

 function FadeOutAfterTime(time : float){
   yield WaitForSeconds(4);
   yield Fade();
 }

 function Fade(){
   while (color.a > 0)
 
   // slowed the fade out
   {color.a -= Time.deltaTime/3;
   yield;
   }
  }

And this one I use for the collider trigger:

 function OnTriggerEnter (col : Collider) {

 FadeUI.Greeting = true;
 }

Relatively straight forward i'm sure to many however I'm just wondering what i'm missing since it works fine once but not a second time when i walk back into the trigger.

I've tried various things, indeed i can turn the GUI label on and off effectively with an exit and enter collider quite easily (hence the booleon) but was wanting a nice fade effect.

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 · Aug 06, 2011 at 11:25 AM

Ehm, quite obvious :D
You start your coroutine in Start() so it is called only once. You should start a new coroutine and you also have to reset the color when it happens again.

 var Greeting : boolean;
 var color : Color;
 
 function Start()
 {
     ShowGreetings();
 }
 
 function OnGUI()
 {
     if (Greeting)
     {
         GUI.color = color;
         GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello");
     }
 }
 
 function ShowGreetings()
 {
     Greeting = true;
     color = Color.white;
     yield WaitForSeconds(3);
     while (color.a > 0)
     {
         color.a -= Time.deltaTime/3;
         yield;
     }
     Greeting = false;
 }

Just call ShowGreetings if you want to show them again. If you want to call it from another script you either send a message with SendMessage to the object or use

 GetComponent.<FadeUI>().ShowGreetings();

The easiest way is to give the script on the trigger a public var in which you can drag&drop your "FadeUI script":

 var myFadeScript : FadeUI;
 
 function OnTriggerEnter (col : Collider)
 {
     myFadeScript.ShowGreetings();
 }
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 martingee · Aug 06, 2011 at 11:50 AM 0
Share

Nice one, thanks for the fast reply! :)

avatar image
0

Answer by aldonaletto · Aug 06, 2011 at 12:01 PM

It's better to change the whole thing - a coroutine isn't the best choice in this case. You must detect when Greeting changes to true, then start the fading process. When the fading ends, clear Greeting, so it will be ready for the next time:

static var Greeting : boolean = false; private var lastState: boolean = false; private var startFade: float = 0; var color : Color;

function OnGUI () {

if (Greeting != lastState){ // has Greeting changed? lastState = Greeting; // update lastState if (Greeting){ // if Greeting changed to true color = Color.white; // reload color startFade = Time.time + 4; // and define time to start fading } } if (Greeting) { GUI.color = color; GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello"); } }

function Update(){

if (Greeting && Time.time > startFade){ // is time to fade? if (color.a > 0){ // fade to zero with time // slowed the fade out color.a -= Time.deltaTime/3; } else { Greeting = false; // when fading ended, reset Greeting } } }

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 martingee · Aug 06, 2011 at 06:03 PM 0
Share

Thanks aldonaletto, i had wondered if there was another way to do what i was trying, the previous answer i reference in my question obviously did what the poster wanted to do and just about did what i wanted.

I'll certainly be trying out both solutions, cheers guys.

avatar image aldonaletto · Aug 06, 2011 at 07:23 PM 0
Share

Don't $$anonymous$$d, now you have two different solutions to the same problem. Both answers probably will do what you want, with $$anonymous$$or differences: @Bunny83's is more elegant and concise, and kept coroutines like the original idea, while $$anonymous$$e kept the control by the Greetings variable, but became more cumbersome.

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

FadeIn/FadeOut GUI Menu and Text 1 Answer

Repeat fade in and out 1 Answer

slowly make gui image more transparent 2 Answers

Can I get a prefab to render in front of GUI buttons created in script? 1 Answer

Fade GUITexture 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