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 JackTenSeven · Aug 04, 2017 at 03:49 AM · imagealphaienumeratorupdate functioncrossfade

UI Image CrossFadeAlpha IEnumerator

I want to make a image fade in then fade out when fuel reaches a certain amount.

In my update function i have

 if (Fuel.value <= 20) {
                 StartCoroutine (LowFuelFadeInOut ());
             }

Then the IEnumerator is

 IEnumerator LowFuelFadeInOut ()
     {
 
             LowFuel.gameObject.SetActive (true);
         //FADE IN
             LowFuel.CrossFadeAlpha (1f, 1.5f, false);
             yield return new WaitForSeconds (2f);
         //FADE OUT
             LowFuel.CrossFadeAlpha (0f, 1.5f, false);
             yield return new WaitForSeconds (2f);
 
         //FADE IN AGAIN
             LowFuel.CrossFadeAlpha (1f, 1.5f, false);
             yield return new WaitForSeconds (2f);
         //FADE OUT AGAIN
             LowFuel.CrossFadeAlpha (0f, 1.5f, false);
             LowFuel.gameObject.SetActive (false);
     
     }

The problem is that the IEnumerator is called again over and over every frame, so after the first fade in it tries to do the fade out and also the fade in again. So what happens to the image is that the image stays at the same alpha and slightly flickers.

Note: I need to be checking the fuel constantly or at least check the value each time it changes so taking the coroutine out of update (or similar) is not an option.

Does anyone have any ideas on what I could do so that the coroutine is only called once?

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
0

Answer by JackTenSeven · Aug 04, 2017 at 03:54 AM

Had a idea. May be possible to make the update check for a certain value that the fuel going down will hit. But my Fuel right now is a float so it is hard to find a certain value. I could change the fuel from float to a int and just increase the int number of total fuel so it goes down in the same speed.

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

Answer by thepopil · Aug 04, 2017 at 04:05 AM

If you want to make sure the StartCoroutine doesn't get called repeatedly, you can set a flag while it's being run. Declare "bool isFuelAnimating = false;" or something in your class and do something like this:

  if(!isFuelAnimating)
      StartCoroutine (LowFuelFadeInOut ());

and then

  IEnumerator LowFuelFadeInOut ()
  {
          isFuelAnimating = true;

          // Do stuff

          isFuelAnimating = false;
  }
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

68 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

Related Questions

Dynamically Remove White - Replace with Alpha 1 Answer

Fade in text one character at a time 3 Answers

How do you set a texture's `alpha is transparency` at runtime? 0 Answers

Blending two Texture2D 1 Answer

Change image alpha of a raw image component stored as a variable 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