- Home /
Changing individual color of particles by C# script NOT Working?
Hi, Below is my code and it is not changing any of the individual particle's color.
particle system component is successfully coming in, and I have found good enough amount of particle to update but no color changes.. any suggestions?
void Update()
{
sys = GetComponent<ParticleSystem>();
ParticleSystem.Particle[] ps = new ParticleSystem.Particle[sys.maxParticles];
int count = sys.GetParticles(ps);
//Debug.Log(count + ":::" + c);
for (int i = 0; i < count; i++)
{
ParticleSystem.Particle p = ps[i];
float LifeProcentage = (p.remainingLifetime / p.startLifetime);
p.startColor = Color.Lerp(Color.clear, Color.red, LifeProcentage); ;
}
sys.SetParticles(ps, count);
}
Thanks!
Answer by gjf · Oct 19, 2016 at 08:57 PM
seems like you're changing the color of the copy of each particle in your loop. you're not modifying the contents of ps[i]
...
oh I though it was returning reference or pointer.. good to know!
i didn't try it, but looking at the code, it's most likely the cause...
Just as a note, this happens because ParticleSystem.Particle is a struct. Structs in C# (as opposed to classes) are value types, so when you do ParticleSystem.Particle p = ps[i];
, you're actually making a deep copy of the particle data, not a reference to the same one particle data. Hope this helps someone :)