How can I set "Random Between Two Colors" for startColor in the particle system by script? I couldn't find documentation on it.
There is an answer to this kind of questions that explains how to do it on the editor.... but who cares about editor? you can edit it by hand.. its the "not needing to build 100 different particle systems" the issue, so not having the second color available sucks. also, i cant figure for the life of me, how the second color would mean a hazard on security that the first color isnt....
Well this question is already a year old, but still only the editor solution is available... since I had the question myself and got here looking for an answer, here is how I figured it out, far more simple and for those who may have the question too:
//Grab a MinMaxGradient, or create a new one and assign its values
ParticleSystem.MinMaxGradient randomColorGradient = new ParticleSystem.MinMaxGradient();
randomColorGradient .mode = ParticleSystemGradientMode.TwoColors;
randomColorGradient .minColor = Color.blue;
randomColorGradient .maxColor = Color.red;
//Assign the gradient back to the ParticleSystem
ParticleSystem.MainModule mainModule = particleSystem.main;
mainModule .startColor = randomColorGradient ;`
Answer by TBruce
Apr 08, 2016 at 11:08 PM
Unfortunately this can not be done as these are private variables. You may be able to do this wile running the game within the editor using SerializedObject and FindProperty. But here is a sample script to show you how it can be done while in the editor (maybe one day Unity will makit so that we can do this at runtime but it is doubtful as it probably makes the game less secure).
public class TempScript : MonoBehaviour
public ParticleSystem particleSystem;
void Start ()
SerializedObject so = new SerializedObject(particleSystem);
if ((so.FindProperty("InitialModule.startColor.minColor") != null) && (so.FindProperty("InitialModule.startColor.maxColor") != null))
Color colorStart = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
Color colorEnd = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
so.FindProperty("InitialModule.startColor.minColor").colorValue = Color.Lerp (colorStart, colorEnd, 1);
colorStart = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
colorEnd = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
so.FindProperty("InitialModule.startColor.maxColor").colorValue = Color.Lerp (colorStart, colorEnd, 1);
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
46 People are following this question.
ParticleSystem not rendering after changing startColor
How do you change the colour of individual particles having assigned them a material?
ParticleAnimator doesn't change color of particles in ParticleSystem(Shuriken)
Changing two different objects renderer colour
Change startcolor of particle system through script