• 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 unicorn_lova · Mar 13, 2018 at 11:01 PM · alphacrossfade

Color alpha for images in an array fail to change...

I am making a health bar that floats above a player that you shoot. It is composed of two images a red background and a green foreground that changes size with the health of the player. Most of the time i want the alpha of both images to be 0 so the health bar can not be seen, but after the player is shot i want to set both alphas to 100 wait 2 seconds and then fade it back to 0.

The code I have below is called from a script that determines if a object hit by a ray cast is tagged player and if so calls FadeOut on the canvas that is a child of the hit player.

     public IEnumerator FadeOut()
     {
         CanvasRenderer[] healthbars = GetComponentsInChildren<CanvasRenderer>();
 
         foreach (CanvasRenderer bar in healthbars)
         {
             Color temp = bar.GetComponent<Image>().color;
             temp.a = 100;
             bar.GetComponent<Image>().color = temp;
             yield return new WaitForSeconds(2);
             bar.GetComponent<Image>().CrossFadeAlpha(0, 1f, false);
         }
     }

This works (although for some reason the background appears and fades out and the foreground does the same with a delay from when the first starts) but it only works once. i thought the part where i set temp to 100 and then set the image color to temp would make it appear again after it already faded out. i can tell that it is still running on both bars when i add Debug.Log(healthbars) it shows 2 objects in the console every time i shoot a player. What i am asking is for help resetting the alpha to 100 when a player is shot for the second time.

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

1 Reply

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

Answer by Summit_Peak · Mar 14, 2018 at 12:18 AM

Color alpha is between 0 and 1, not 0 and 255. Try:

 temp.a = 100 / 255.0f;
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 unicorn_lova · Mar 14, 2018 at 01:49 AM 0
Share

Thank you very helpful information but it turns out that the issue in the code was somewhere else...

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

74 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

Related Questions

Stop crossfadealpha 1 Answer

How to have CrossFadeAlpha activate more than once when pressing a key? 0 Answers

UI Image CrossFadeAlpha IEnumerator 2 Answers

Texture loading problem: size, aspect ratio, and alpha 0 Answers

Adding shadows to transparent shader? 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