How to Slowly Fade Particle System via Script

Okay, I’ve got a looping particle system that emits expanding donuts of particles (in bursts), and already uses Color over Lifetime to change their color and fade them out shortly before their lifetime ends.

However, I also want to make the entire particle system slowly fade out at a rate defined in a script (the system would be attached to a shotgun-like attack, and the fading would represent the diminishing damage potential of the attack). However, I can’t seem to find a way to do this. Any ideas?

You would need to access the particle system by attaching a monobehavior script to it. You can get access to it using:

//This sets up an array container to hold all of the particles
ParticleSystem.particle[] particles = new ParticleSystem.Particle[particleSystem.particleCount];

//get the particles

//then iterate through the array changing the color 1 by 1
for(int p = 0; p < particles.Length; p++)
    particles[p].color = new Color32(r, g, b, a);//where a = the alpha

//set the particles back to the system
particleSystem.SetParticles(particles, particles.Length);

This will be done in the Update method and would allow the uniform fade for all particles in the system I think you are looking for. This code is off the top of my head so it may not be perfect, but should give you the correct idea and syntax.

You would need to either have a global alpha variable and decrement it over time and apply it to the new Color32 each frame or get the current particle color and decrement the alpha overtime.

I was looking for an answer to this same problem, but the solution above cost too much cpu power for my particular instance. Instead, I decided to change the start color of the particle system over time, rather than go through every single particle. This worked in my case, as the particles were moving and emitting so fast that it gave the same effect…Anyways, here is some sample code…Note that ps.startColor is deprecated now, but you get the idea…

private void fadeParticleSystem()
        Color col = deathRayOrbPs.startColor;
        Debug.Log("Initial Color: " + col.a);
        col.a -= col.a * colorFadeDampner * Time.deltaTime;
        Debug.Log("Color: " + col.a);
        deathRayOrbPs.startColor = col;