- Home /
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"?
Answer by ifurkend · Mar 24, 2017 at 12:16 PM
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: https://blogs.unity3d.com/en/2016/04/20/particle-system-modules-faq/
Thanks for the info! However, strangely my problem persists because the $$anonymous$$ain$$anonymous$$odule-Type can't be found:
ParticleSystem.$$anonymous$$ain$$anonymous$$odule psmain = myParticleSystem.main;
Results in: "error CS0426: The nested type $$anonymous$$ain$$anonymous$$odule
does not exist in the type UnityEngine.ParticleSystem
".
$$anonymous$$y Unity version is 5.4.2f2 - but all $$anonymous$$odule-Types of ParticleSystem I have access to are Collision$$anonymous$$odule, ColorOverLifetime$$anonymous$$odule, Emission$$anonymous$$odule, ExternalForces$$anonymous$$odule, ForceOverLifetime$$anonymous$$odule, InheritVelocity$$anonymous$$odule, LimitVelocityOverLifetime$$anonymous$$odule, RotationBySpeed$$anonymous$$odule, RotationOverLifetime$$anonymous$$odule, Shape$$anonymous$$odule, SizeBySpeed$$anonymous$$odule, SizeOverLifetime$$anonymous$$odule, SubEmitters$$anonymous$$odule, TextureSheetAnimation$$anonymous$$odule, Trigger$$anonymous$$odule, VelocityOverLifetime$$anonymous$$odule.
No sign of the $$anonymous$$ain$$anonymous$$odule... am I doing something wrong?
If I try
var psmain = myParticleSystem.main;
I get "Type UnityEngine.ParticleSystem
does not contain a definition for main
and no extension method main
of type UnityEngine.ParticleSystem
could be found (are you missing a using directive or an assembly reference?)
Surprise surprise, there is no "$$anonymous$$ain$$anonymous$$odule" in 5.4. The API of 5.4 also provides absolutely no script example for $$anonymous$$in$$anonymous$$axCurve which works totally different from 5.5. For heaven's sake, upgrade to 5.5 in which our initial answers apply.
That is... confusing. :D
I'm kind of scared to upgrade right in the middle of a project, so I guess I'll either assign the Randomness "manually" or, yeah, do the upgrade.
BTW, if I just assign a constant, I don't need the main$$anonymous$$odule at all, it works simply as
myParticleSystem.startLifetime = 3.5f;
Seems kind of strange to me...
Answer by SohailBukhari · Mar 24, 2017 at 12:09 PM
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;
}
}
That does not help me too much because then still all particles have the same lifetime (it is only randomly chosen once). But thanks anyway!
Before assigning you should choose Random value for each instantiating particle.I write code in Start() so value choose only once.Change this code as you want.
Call This Will Return Random Time For each Particle.
var initialTime = RanTime();
private float RanTime()
{
float rantime = Random.Range(_$$anonymous$$, _max);
return rantime;
}
Yeah, I could constantly change the startLifetime of the ParticleSystem in Update() or of each emitted Particle but that seems unpractical to me, since the API to let the ParticleSystem handle the Randomness is appearantly there (since it works in the inspector) and I would guess it would handle it more efficiently than this "manual" approach.
So this would only be my "last resort" approach...
Thanks. Best way to handle is unity editor for particles system ins$$anonymous$$d of using update make your Thread an instantiate in couroutine.
Your answer
Follow this Question
Related Questions
Dynamic Particle System Lifetime?? 2 Answers
Destroy particles based on bounds not lifetime 0 Answers
Unity 5.3.4 - Particles from Prefab will not instantiate. 0 Answers
How to keep particles moving past an obstacle after they have collided with it? 3 Answers
Particle System Working in game view but not working on Android build. 1 Answer