Changing color of Particles in C#? Please help

Hi,

I need to color the particles that emit from my Game Object. The Game Object has a prefab attached to it which have the size, min, max, etc. of all the particles. Their is also an Ellipsoid Particle Emitter as one of the components to the Game Object.

How can I loop through the array of particles to change them from white to red?

Below is not the entire code, but the most relevent.

public GameObject ParticlePrefab;
public static GameObject instantiated;
	

if (Input.GetKeyDown("h")  instantiated == null)
{
    
    instantiated = (GameObject) Instantiate(ParticlePrefab, transform.position, Quaternion.identity);
			
			
			Particle[] p = particleEmitter.particles;
			
			for (int i = 0; i < 20; i++)
			{
				p[i].color = Color.red;
			}
}

What happens, is that whenever I press h on the keyboard, the particles emit as they should. I just want to loop through the particles that are instantiated when I press ‘h’ and change them from white to red.

So yeah, Im going to see if I can buy that particle tutorial that I saw earlier.

After your FOR loop add the following line:

particleEmitter.particles = p;

The reason is you actually made a copy of the particle emitter’s array, and were modifying the color of the particles in the copy, not in the original. Assigning the copy back to the original at the end makes sure your changes get pushed back into the particleEmitter and from there on screen via the ParticleRenderer.