- Home /
changes to colour alpha affect RGB values, but not in the same way
Firstly, I'll let you know that this script is currently functioning correctly - that is, the ballMaterial's alpha value fades to 0.3 while the camera is within a certain distance of the ballObject. My issue is that without the "ballColor.r", "ballColor.g" and "ballColor.b" lines in place, the RGB colour of the ballObject's material changes to (0,0,0) and stays there throughout play and even after play mode has been stopped. I simply don't understand how the RGB is getting changed to black when I only have a line modifying the alpha channel! At the very least I'd expect the RGB to fade between black and white with the alpha, but no...
If I'm doing something slightly wrong, it would be handy to know - and if this is just the way it works, is there a more elegant way of overriding the RGB? The multi-line solution I have at the moment seems really clumsy!
void FixedUpdate()
{
if (Vector3.Distance(ballCamera.position, ballObject.position) < fadeDistance)
{
FadeBall(0.3f);
}
else
{
FadeBall(1.0f);
}
}
void FadeBall(float targetOpacity)
{
ballColor.a = Mathf.Lerp(ballColor.a, targetOpacity, /*Time.time **/ fadeDamping);
ballColor.r = 1;
ballColor.g = 1;
ballColor.b = 1;
ballMaterial.color = ballColor;
}
Answer by Jessy · Jul 11, 2011 at 04:52 PM
You haven't posted where ballColor comes from, but your problem is that it's a newly-created Color every time FadeBall is run. floating point variables are zero upon creation, and Color is a structs that contains four of them. Setting ballColor to be ballMaterial.color at the beignning of the funtion will solve the problem, but if you're bothering to store the variable, you probably should fix your code so that you're not creating a new Color for it all the time. I recommend keeping a class with extension methods around, and having a method like SetAlpha in there:
public static void SetAlpha (this Material material, float value) {
Color color = material.color;
color.a = value;
material.color = color;
}
void FadeBall(float targetOpacity) {
ballMaterial.SetAlpha(Mathf.Lerp(ballColor.a, targetOpacity, whatever));
ballColor = ballMaterial.color;
}
Spot on regarding the colour issue!
I'm not too comfortable with C# yet, still learning some basic concepts elsewhere at the same time as winging it in Unity - do you mean keeping extension methods in a class in the same script, or in another one in the project? And if my guesstimation is correct, would that behave sort of like a macro?
See, this is the kind of stuff I need to be learning, and it's unfortunately the kind of stuff that the Unity documentation doesn't go into!
Your answer
Follow this Question
Related Questions
Change alpha value to transparent when colliding 1 Answer
How to change the alpha of a spawned particle? 1 Answer
Cannot set UnityUI.Image opacity when place on a UnityUI.Button 2 Answers
Transperency not working correctly on imported objects. 1 Answer
Mesh transparency halfway cutoff script 0 Answers