- Home /
ParticleSystem Random Between Two Colors
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 $$anonymous$$in$$anonymous$$axGradient, or create a new one and assign its values
ParticleSystem.$$anonymous$$in$$anonymous$$axGradient randomColorGradient = new ParticleSystem.$$anonymous$$in$$anonymous$$axGradient();
randomColorGradient .mode = ParticleSystemGradient$$anonymous$$ode.TwoColors;
randomColorGradient .$$anonymous$$Color = Color.blue;
randomColorGradient .maxColor = Color.red;
//Assign the gradient back to the ParticleSystem
ParticleSystem.$$anonymous$$ain$$anonymous$$odule main$$anonymous$$odule = particleSystem.main;
main$$anonymous$$odule .startColor = randomColorGradient ;`
thanks for sharing, Loled85. I don't think this solution is possible pre Unity 5.5. just leaving this note here for anybody still using old versions (like me)
Answer by TBruce · Apr 08, 2016 at 11:08 PM
@jpierre88 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).
using UnityEngine;
using System;
using System.Collections;
using UnityEditor;
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);
so.ApplyModifiedProperties();
}
}
}
Sorry. You are wrong now.
var grad = new ParticleSystem.$$anonymous$$in$$anonymous$$axGradient(AshenColorfulGradient);
grad.mode = ParticleSystemGradient$$anonymous$$ode.RandomColor;
it is doubtful as it probably makes the game less secure
Security has nothing to do with marking variables private. A private variable is not supposed to be accessed to enforce Object Oriented principles, but you can still access it using reflection. You just shouldn't.
Answer by petersvp · Jun 30, 2018 at 08:58 PM
The ParticleSystem.MinMaxGradient
struct has a mode
field. You cas setup the RandomColor
value for this field. Like in this code:
ParticleSystem.MinMaxGradient grad = new ParticleSystem.MinMaxGradient(Color.red, Color.blue);
grad.mode = ParticleSystemGradientMode.RandomColor;
// then set this grad variable to some particle module
mainModule.startColor = grad;
And you have to fill that either into a single gradient or via the two colors constructor.
I can't seem to get this to work. Wondered if anything has changed, or if you happen to have found a better method to accomplish the random start color. Thanks.
check out this post from yesterday
Tried this and the particles only come out white, not one of the two colors I'm specifying. Any ideas?
You don't need to set the gradient mode, remove that and it should work
Answer by Tekksin · Sep 20, 2018 at 06:50 AM
If anybody wants to use Petersvp's method, you can try, but I think its been deprecated. Using his code I got an error, but the error vanished when I did this:
public Color[] theColor;
private ParticleSystem ps;
void Start(){
ps = GetComponent<ParticleSystem> ();
ParticleSystem.MinMaxGradient grad = new ParticleSystem.MinMaxGradient (theColor [0], theColor [1]);
grad.mode = ParticleSystemGradientMode.RandomColor;
var main = ps.main;
main.startColor = grad;
}
At the end of the day, I think it's redundant to use startcolor at all, because color overlifetime accounts for the very start and through to the end. If you want just one color, then just set the color over lifetime to the same color twice.
var col = ps.colorOverLifetime;
grad.SetKeys (new GradientColorKey[] {
new GradientColorKey (color.blue, 0.0f),
new GradientColorKey (color.blue, 1.0f)
}, new GradientAlphaKey[] {
new GradientAlphaKey (1.0f, 0.0f),
new GradientAlphaKey (1.0f, 1.0f)
});
col.color = grad;
hope this helps somebody.
Nothing is deprecated, it's just how your code is structured.
The debate of StartColor vs ColorOverLifeTime are two separate features. The base color that a particle will be spawned, and then its change over the particle's lifetime. In StartColor you can only feed white with some alpha difference, while the Color Over Lifetime - a gradient that fully disappears, for example. Both StartColor and ColorOverLifetime have their per-particle feature control that is important for some effects.
OP's question was generic, Random between two colors one, and ParticleSystemGradient$$anonymous$$ode.RandomColor
is its answer :)
Your answer is, from my point of view, logically same as $$anonymous$$e :) just more full-fledgen example. in my snippet, "main$$anonymous$$odule" is something undefined, and .startColor is just example usage. He can set this $$anonymous$$in$$anonymous$$axGradient
on any module, any supported Color-like property :)
Your answer
Follow this Question
Related Questions
how to emit particle system attached to object in code? 1 Answer
How can I use 'curves' like ParticleSystem has? 1 Answer
How do you change the colour of individual particles having assigned them a material? 1 Answer
How do I change Particle System color via script in Unity 5.6.1f1? 1 Answer
Change startcolor of particle system through script 1 Answer