- Home /
Can I change particle color over time with script?
I want to change the color of particles emitted by an emitter, but I want to change the color that comes out over time. I tried this with legacy particles, setting all 5 colors in the color animator, but it changes all the particle colors. I just want to change the ones being emitted 'right now' and let the ones that already exist keep their color(s) until they die off. Is that possible, maybe with Shuiken? Need to use script, not editor/animator.
Answer by Screenhog · Sep 06, 2012 at 10:26 PM
Good news, I got it working. I added this script to a Shuriken Particle System. It's not exactly what you want, but it proves that the initial color of the particles can be changed over time:
var ps: ParticleSystem;
var psColor: Color;
function Start () {
ps = GetComponent (ParticleSystem);
}
function Update () {
psColor = Color.Lerp(Color.blue, Color.red, Time.time/5);
ps.startColor = psColor;
}
Yeah that will change the entire particle set to the new color right? And actually, if I have a gradient set for colorOverLifetime, startColor acts to tint that. So I should leave that white. I'm thinking I need to Get/Set particles and grunt out the colors and hope the system doesn't override them.
No, each particle as it comes out will be assigned a color, and it will stay as that color, even as the psColor value changes. Don't touch colorOverLifetime (unless I'm misunderstanding the effect you want).
Is there a script that would please change the particle colour over Y axis? So as each particle rises up in the Y axis, the colour changes appropriately
Answer by Screenhog · Sep 06, 2012 at 09:07 PM
Well, it's easy to do in the editor. By scripting, I don't know.
You access a Shuriken particle system's code through ParticleSystem.startColor. However, that just asks for an initial color, not a gradient of colors (which is what you would assign in the editor). My only thought would be that you constantly change ParticleSystem.startColor over a period of time, perhaps through Color.Lerp. I haven't attempted it myself, but that's the direction I'd go.
Interesting, I will try but I worry that like with the Legacy particles, I would have to queue the colors, moving the last start color up the line as I change the new start color. Which was not looking too good for me. Actually trying this just now in the editor, I set particle lifetime to 20 seconds, then change the start color. It does change the color of all particles, not just newly-created ones.
Well, I know that when a gradient is assigned to the start color, it does change over time. But I don't think you can script it to be a gradient.
Right, if I could set the gradient for colorOverLifeTime I could at least animate that. I would hate to have to Get/Set particles. $$anonymous$$ight as well write my own system.
Answer by Charles-Van-Norman · Feb 12, 2017 at 01:06 AM
you can change color over lifetime in Shuriken, it's now covered in the DOCS:
https://docs.unity3d.com/ScriptReference/ParticleSystem-colorOverLifetime.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613080724im_/https://answers.unity.com/themes/thub/images/avi.jpg)