How do I set ParticleSystem.startLifetime by Curve in script ?

According to below ParticleSystem.startLifetime reference, ParticleSystem.startLifetime can set value by “Curve”.

In the Unity Inspector, I can set its value by following 4 ways.

  • Constant
  • Curve
  • Random Between Two Constants
  • Random Between Two Curves

But in C# script, I can’t find the way except for Constant.

How do I set its value by “Curve”, “Random Between Two Constants” or “Random Between Two Curves” ?

For example following codes has error "Error : Cannot convert from UnityEngine.AnimationCurve to System.Single ".

var particleSystem = gameObject.AddComponent<ParticleSystem>();
var curve =new AnimationCurve();
particleSystem.startLifetime = curve;

float amplitude = 1.0f;
AnimationCurve curve = new AnimationCurve ();
particleSystem.startLifetime = new ParticleSystem.MinMaxCurve (amplitude, curve);

Hope this works for you.

instead of directly changing your curve through script, you could make the script activate an animation that’ll then change the curve. By doing this you have full control to decide when the curve is activated. I hope this helps and im sorry for being so short.

for a randomization i suppose you could do multiple animations and have the animations activated between float integers in the parameters section, then have the float be randomized in your script.