Select a random color preset in the particle system through C#
Hi everyone !
I have a question, I create a particle system that generates particles with random colors according to a color preset that I created. However I created several color preset and I would like this particle system to choose a different preset each time I activate it (for example every time I press button play). I find a topic that speaks about it here, but I do not really understand how this person does it and I wonder if there is not an easier or optimized way.
I hope you will understand me, thank you !
Answer by richardkettlewell · Aug 20, 2019 at 10:38 AM
Hey, I think you want to assign your preset to the startColor property of the particle system before playing it, using this API: https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule-startColor.html
Rather than assigning a fixed color, you want to assign a MinMaxGradient that is in the "fixed" mode. There is an example close to what you want here: https://docs.unity3d.com/ScriptReference/ParticleSystem.MinMaxGradient.html Use the example with the title "// This example shows using a gradient to drive the color over lifetime." but set the mode to fixed by calling ourGradient.mode = GradientMode.Fixed;
This means colors will be selected with no blending, giving you a preset list of colors for the particles.
Create a few MinMaxGradients, where each one represents one of your presets, then choose which one to assign to the startColor property before calling Play.
Hope it helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612210913im_/https://answers.unity.com/themes/thub/images/avi.jpg)