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;
}

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;
}