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 Quelandoris · Mar 30, 2019 at 06:13 AM · guispriteimagealpha

Why is the alpha on my UI Image not decreasing properly?

I've been at this for an hour now and I'm starting to lose my mind.

trying to make a simple blood overlay that is set active by an outside object, that reduces in alpha until its at 0 alpha, at which point it disables itself. Instead, it seems to only do two states for the alpha: fully on, or fully off. It disables and enables like it should, and even seems to detect when the Alpha should be 0 to turn itself off, but it doesn't do a proper fade-out.

Here's my code

 public float alphaReduce=1f;
     public Image myImage;
     void OnEnable()
     {
         Color c = myImage.color;
         c.a = 255;
         myImage.color = c;
     }
     void Update () {
         Color c = myImage.color;
         c.a =c.a - alphaReduce;
         myImage.color = c;
         if (myImage.color.a <= 0) gameObject.SetActive(false);
     }


Any help is appreciated.

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
Best Answer

Answer by fafase · Mar 30, 2019 at 09:40 AM

You are decreasing value without considering the performance of the computer.

 float alphaReduce = 1f;
 void Update () {
      Color c = myImage.color;
      c.a =c.a - alphaReduce * Time.deltaTime;
      myImage.color = c;
      if (myImage.color.a <= 0) gameObject.SetActive(false);
  }

this will take 1s to complete if c.a is 1.

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
1

Answer by mchts · Mar 30, 2019 at 08:00 AM

Remember that Unity's build in Color takes r,g,b,a 0 to 1. I wrote sth like this:

 public class Fade : MonoBehaviour {
 
     public Image myImage;
     public Color fadeColor; //you could arrange fade color from inspector too i assigned it manually here
 
     void Start () {
         //this line here is unnecessary if you assign fadeColor from inspector
         fadeColor = new Color(myImage.color.r, myImage.color.g, myImage.color.b, 0f);
     }
     
     void Update () {
         myImage.color = Color.Lerp(myImage.color, fadeColor, Time.deltaTime);
         if (myImage.color.a < 0.02) { //having a threshold is a better option
             gameObject.SetActive(false);
         }
     }
 }

Comment
Add comment · Show 4 · 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 fafase · Mar 30, 2019 at 09:46 AM 1
Share

You are misusing Lerp in this context. The parameter you are passing are more appropriate for $$anonymous$$oveTowards method.

   Color.Lerp(start, end, rate)

actually, since only the alpha is processed, $$anonymous$$athf.Lerp would be better. But then you need to control the rate so it reaches 1 to fulfil the interpolation.

So either:

   Color c = myImage.color;
   c.a = $$anonymous$$athf.$$anonymous$$oveTowards(c.a, 0f, step * Time.deltaTime);
   myImage.color = c;

or:

   elapsedTime += Time.deltaTime;
   c.a = $$anonymous$$athf.Lerp(1f, 0f, elapsedTime / speed);
   myImage.color = c;

 
avatar image mchts fafase · Mar 30, 2019 at 11:41 AM 0
Share

For just changing alpha value your approach is more suitable for sure, but like i stated in the comment section i used Color.Lerp in terms of final color might be different than initial image color.

avatar image fafase mchts · Mar 31, 2019 at 08:06 AM 1
Share

$$anonymous$$y point was not necessarily about the Color or $$anonymous$$athf but more about the fact you are misusing the parameters. What you're doing with Lerp is creating a limit process where the movement will never reach the destination. To hack that issue, you add the 0.02 check. You need to get your rate to reach 1 to use Lerp probably.

Show more comments

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

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

Unity 5.0 Trying to Change the Image - Source Image via Script 3 Answers

Draw a rotated image on GUI 1 Answer

How to stop a sliced image from shrinking with the container? 0 Answers

Help with destroying guiRect? 0 Answers

What is the name of .color propert for new Unity UI Image? 3 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