Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jwulf · Mar 24, 2017 at 11:55 AM · c#particlesparticlesystemparticle systemlifetime

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"?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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/

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jwulf · Mar 24, 2017 at 12:39 PM 0
Share

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?)

avatar image ifurkend jwulf · Mar 24, 2017 at 01:06 PM 0
Share

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.

avatar image jwulf ifurkend · Mar 24, 2017 at 01:58 PM 0
Share

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.

Show more comments
Show more comments
avatar image jwulf · Mar 24, 2017 at 12:41 PM 0
Share

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...

avatar image
2

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;
         }
     }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jwulf · Mar 24, 2017 at 12:42 PM 0
Share

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!

avatar image SohailBukhari · Mar 24, 2017 at 12:49 PM 0
Share

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.

avatar image SohailBukhari · Mar 24, 2017 at 12:52 PM 0
Share

Call This Will Return Random Time For each Particle.

  var initialTime = RanTime();
  private float RanTime()
  {
         float rantime = Random.Range(_$$anonymous$$, _max);
         return rantime;
   }





avatar image jwulf SohailBukhari · Mar 24, 2017 at 01:00 PM 0
Share

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...

avatar image SohailBukhari jwulf · Mar 24, 2017 at 01:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

298 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges