Unable to change particle energy variable.

Yo commUnity,

What I’m trying to accomplish with a particle emitter is to dictate a specific ‘tile’ of the UV animation to display.

As I understand, the UV animation is tied to the particle energy and will animate through the defined number of cycles while the energy decreases to zero. I found this related old ‘Wish List’ post:

http://forum.unity3d.com/viewtopic.php?p=21319&sid=c5acd876e0376a2eef07f4230afc3555

The solution there is to change the energy variable of the particle which should, in turn, set the appropriate tile from the material.

Any script change to the energy variable basically gets reset and ignored. Logging the energy during the update call I change it in shows the change, but it consistently gets reset before the next update and the UV Animation continues as if nothing has changed.

Help? Thanks in advance!

Have you tried changing the energy value in LateUpdate?

Alternatively, if you delete/disable the ParticleAnimator Unity won’t change the particles and you can have full control of them in code.

Yes, I had tried the code in Update, FixedUpdate, LateUpdate, just to be sure. Good idea on the particle animator, however that simply stops the particle system from decreasing it’s own energy. I found the value still kept resetting, just to the original energy instead of a decremented energy.

Fortunately, yet painfully, the punishing backhand of self-FAIL coding has struck across my face once again …

I was also following guidance from the scripting reference on ParticleEmitter.particles so I knew this SHOULD work.

Problem was I had this line:
// extract the particles
var particles = particleEmitter.particles;

Without this one after the changes:
// copy them back to the particle system
particleEmitter.particles = particles;

It now accomplishes what I wanted, of course manually altering the energy to display my desired tile on the sprite sheet meant that the particle never gets killed. I’ve worked around that by setting the energy to near zero when I’m done with it and letting the ParticleAnimator do the rest. I’ll keep cleaning things up and then try to remember to post another thread with a few tricks I’ve learned in case they come in handy to others.

Thanks for the help, Sycle.