- Home /
How to change particle system's "color over lifetime" by script?
Simply description of my question:
I have a particle(attached a particle system to an empty game object) and I already set the "Color over Lifetime" attribute in inspector. Now I want to reset the "Color over lifetime" to another pattern after an event is triggered.
the problem is that: I can't find any variables and functions could be used directly finish the job. Is there any?
I tried to attach ParticleAnimator to the same gameObject but it doesn't work. Is there any good way to solve the problem? Or should I get all the particles emitted by the system and adjust them one by one in my script?
The pic below shows how i set the ParticleSystem and ParticleAnimator. But the particle remains green and yellow. Did I do something wrong? Help!!! Thank you very much.
[1]: /storage/temp/4918-particleproblem.png
http://docs.unity3d.com/Documentation/ScriptReference/ParticleAnimator-colorAnimation.html - As it says, you can't modify one element of the array currently, you have to set the whole array and then assign it, but that shouldn't be too bad.
Scribe
@Scribe Thank you for your reply. But sorry it didn't solve my problem. I did read the scripting reference of ParticleAnimator. Actually I want to find some way to solve the problem without ParticleAnimator if there is. Since the particle system has the "Color over Lifetime" item, then Why can't I script directly with Particle System interface?
And I haven't tried scripting the Animator yet. Just edited it in inspector as you can see from my pic but the color of my particle didn't change. Does Animator only works by scripting? Then what does the items in inspector do?
@Scribe I just noticed that Particle system and particle Animator seems to be parallel components. I think that's why the animator component I added doesn't work for the particle system. But in docs for the old version of Unity, it saids Particle system is composed of emitter, animator and renderer. But in the latest version of unity the inspcetor interface of particle system changed and when I tried to get the animator component in object attached with Particle system, it returns NULL. Does this mean I can't change the color over life time of particles emitted by particle system with animator component?
Answer by zhuk1011 · Jan 28, 2016 at 01:28 PM
I'm so sorry to say but, this seems does not work for me
I have tried the example code but it gives 2 errors:
colorOverLifeTime is not a definition in ParticleSystem
$$anonymous$$in$$anonymous$$axGradient does not exit
Answer by CodeMasterMike · Nov 14, 2012 at 06:43 AM
Just using script it seems that you cant set the "color over lifetime" color, either for each individual particle, or for the whole particle system.
However, you can set the color for each individual particle, and use your own "color over lifetime" Lerp calculation.
ParticleSystem m_currentParticleEffect = (ParticleSystem)GetComponent("ParticleSystem");
ParticleSystem.Particle []ParticleList = new ParticleSystem.Particle[m_currentParticleEffect.particleCount];
m_currentParticleEffect.GetParticles(ParticleList);
for(int i = 0; i < ParticleList.Length; ++i)
{
float LifeProcentage = (ParticleList[i].lifetime / ParticleList[i].startLifetime);
ParticleList[i].color = Color.Lerp(Color.clear, Color.red, LifeProcentage);
}
m_currentParticleEffect.SetParticles(ParticleList, m_currentParticleEffect.particleCount);
This is probably very expensive if the effect as lots of particles, so its maybe not a suitable sollution for all systems.
This is exactly what I want! Thank you! I think particle system should have the color over lifetime be its interface, which make things a little easier, and maybe better performance?
$$anonymous$$aybe they will add more functionality to the particle system through scripting in later Unity versions?
All variables, including color over lifetime, must be stored somewhere. But simply isn't reachable from a script.
Your answer