- Home /
Random colors for particles?
Hello. I know how you can change the color of particles from a script, how can I make it so that every time the effect is instantiated, the colors are different? Thanks.
Answer by Mendicant · Nov 30, 2010 at 08:39 AM
Sadly due to snow and a lack of a pc nearby, I can't test the following. You could store the particle number in script and then when more particles exist try something like:
particle.color = new Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 255);
See http://unity3d.com/support/documentation/ScriptReference/Particle.html for more info. See http://unity3d.com/support/documentation/ScriptReference/ParticleEmitter-particles.html to learn how to get and set particles from the emitter.
I hope this helps :)
Didn't work for me. when i put in with a Get$$anonymous$$eyUp code it only turns white HELP
@Dzjengis$$anonymous$$han: Don't ask questions as an ANSWER to this question. Answers should answer the question. Use "add new comment" if you want to clarify something.
Beside that, yes you're right, It will most the time return a white color because the Color struct uses values between 0.0 and 1.0
To use byte color values you can use the Color32 struct which converts automatically into a Color struct.
So either use:
// C#
particle.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1.0f);
// UnityScript
particle.color = Color(Random.Range(0.0, 1.0), Random.Range(0.0, 1.0), Random.Range(0.0, 1.0), 1.0);
or
// C#
particle.color = new Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 1);
// UnityScript
particle.color = Color32(Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255), 255);