- Home /
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);
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.
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
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.
I don't know exactly why this works, But thank you it is working well.
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
Follow this Question
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