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 JoshMBeyer · Aug 14, 2014 at 02:19 AM · c#rendereralphaspriterenderer

Why is this "working" but not actually changing anything?

I am trying to make the characters color fade. When accessing the alpha, its not throwing errors, but when I pause the game it remains at 255 when it should be droping. Since the "Health" variable is changing. Health is capped between 0 - 100.

Here is the part of the code.

 public float health;
     public GUIStyle myStyle;
     public bool enable;
     public float multiplier;
 
     void Start()
     {
         health = 100;
         multiplier = 1;
     }
 
     
     void Update () 
     {
         SpriteRenderer renderer = this.GetComponent<SpriteRenderer>();
         renderer.color = new Color(0f, 0f, 0f, health + 155f);
         
Comment
Add comment · Show 1
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 ian_sorbello · Aug 14, 2014 at 02:32 AM 0
Share

I'd first change the code from newing up a new Color object every frame and do this ins$$anonymous$$d:

 renderer.color.a = 155f + health;

Also, are you referring to the same health variable? I don't see it declared at class scope, so if the variable isn't changing, often it's because you're referring to another declared local var accidentally.

1 Reply

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

Answer by Slobdell · Aug 14, 2014 at 02:28 AM

Alpha is a number between 0 and 1. With what you said your alpha will be at minimum 155. Try changing to health / 100

Comment
Add comment · Show 3 · 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 JoshMBeyer · Aug 14, 2014 at 02:31 AM 0
Share

Well when I look at the slider RGBA the all range from 0 to 255 That is why I assumed using the health for 100, and adding 155 to complete it. Being at 155 with my sprite it is invisible.

avatar image JoshMBeyer · Aug 14, 2014 at 02:32 AM 0
Share

I don't know exactly why this works, But thank you it is working well.

avatar image gjf · Aug 14, 2014 at 09:15 AM 0
Share

the slider shows values from 0 thru 255 since that's a more conventional way to refer to each component value and much easier for humans to remember.

programatically, however, values between 0.0 and 1.0 are used, so to scale appropriately you should take your value and divide by whatever the maximum value would be and, in this case, that's probably 100.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Changing sort order of particles on instantiate? (C#) 0 Answers

Making a bubble level (not a game but work tool) 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