Changing Particles Color after Spawn - weird color behaviour

Hey,
I’m using Unity3ds built in particle system to simulate clouds. To do so I spawn all particles at the beginning of the scene and they exist for the complete level time (which works because my levels have a maximum time of appr. 3 mins). As my game is kind of a strategy game with a camera floating (high) above the ground sometimes the clouds obscure the actual level.
What I want to do is fade the clouds out close to invisibility. I tried to do so using the following code:

ParticleSystem pS = Clouds[i].GetComponent<ParticleSystem>();
ParticleSystem.Particle[] pSparticles = new ParticleSystem.Particle[pS.particleCount];
pS.GetParticles(pSparticles);
if(pSparticles.Length > 0)
{
	for(int a = 0; a < pSparticles.Length; a++)
	{
		if(pSparticles[a].color.a > 30f)
		{
			Color returnColor = new Color(pSparticles[a].color.r, pSparticles[a].color.g, pSparticles[a].color.b, pSparticles[a].color.a - 2);
			pSparticles[a].color = returnColor;
		}
	}
	pS.SetParticles(pSparticles, pSparticles.Length);
}

To a certain extend this works. I query the particles, i get the color and change it, apply it back but for some reason the applied color changes to rgba(255,255,255,255), although none of the applied values is 255 (initially).
I’ve got no idea from where this behaviour comes. Any ideas?
Thanks!

1 Like

You’re assigning them a value of new Color but particles use Color32. Color only accepts float values between 0 and 1 which is why it’s getting hosed. Change it to this:

Color32 returnColor = new Color32(pSparticles[a].color.r, pSparticles[a].color.g, pSparticles[a].color.b, pSparticles[a].color.a - 2);

So, the RGBA values of Color are floats and the RBGA values of Color32 are Byte. So for Color your valid values are 0 to 1. While for the Color32 your valid values are 0x00 to 0xFF or 0 to 255.

1 Like

Exactly. Thank you!