Assign random start lifetime for particle system from script

In the inspector, you can simply set two constants as the boundaries for a random StartLifetime for the particles of a ParticleSystem.

However, in script, this does not seem to be possible:

myParticleSystem.startLifetime = new ParticleSystem.MinMaxCurve (minLifetime, minLifetime * .1f);

won’t compile because you can only assign a float to startLifetime. (“Cannot implicitly convert type UnityEngine.ParticleSystem.MinMaxCurve to float”)

Is there a way to assign a value like “random between to constants”?

You Cant Not Change Value Directly Try This will solve Your Problem

 class RandomAssign
    {
        [SerializeField] private ParticleSystem _particleSystem;
        [SerializeField]private float _min ;
        [SerializeField]private float _max;
        private void Start()
        {
            //using Random Value
            var initialTime = Random.Range(_min, _max);
            
            var main = _particleSystem.main;
            //Assign Time Using Curve
           // main.startLifetime = new ParticleSystem.MinMaxCurve(_min,_max);
    
            main.startLifetime = initialTime;
        }
    }

Starting from 5.3 you need to first define a variable for the particle system module in question before using its functions, in this case, the “main module”:

ParticleSystem.MainModule psmain = myParticleSystem.main;
psmain.startLifetime = new ParticleSystem.MinMaxCurve (minLifetime, minLifetime * .1f);

It you feel the extra line to define the struct is totally pointless, write a message for Karl Jones.

ref: Particle System Modules - FAQ | Unity Blog