Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

ParticleSystem .Play()/.Stop() not working properly

Discussion in 'Scripting' started by pbaumgartner98, Mar 8, 2020.

  1. pbaumgartner98

    pbaumgartner98

    Joined:
    Mar 1, 2020
    Posts:
    13
    When I use ParticleSystem.Stop() and then .Play() the particles start spawning 1-2 seconds after the call.
    If I swap .Stop() with .Pause() then it works, but my particles need to be updated after pausing hence I`m using .Stop().

    Start delay is 0.

    Any Idea?

    Does not work:
    Code (CSharp):
    1.         foreach (ParticleSystem p in particles)
    2.         {
    3.             // For debugging
    4.             a = isDrifting && p.isPlaying;
    5.  
    6.             //Particles
    7.             if (isDrifting && driftDir != 0 && p.isStopped)
    8.             {
    9.                 p.Play();
    10.             }
    11.             else if (!isDrifting && !p.isStopped)
    12.             {
    13.                 p.Stop();
    14.             }
    15.         }

    Does work:
    Code (CSharp):
    1.         foreach (ParticleSystem p in particles)
    2.         {
    3.             //For debugging
    4.             a = isDrifting && p.isPlaying;
    5.  
    6.             //Particles
    7.             if (isDrifting && driftDir != 0 && p.isPaused)
    8.             {
    9.                 p.Play();
    10.             }
    11.             else if (!isDrifting && !p.isPaused)
    12.             {
    13.                 p.Pause();
    14.             }
    15.         }
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    When you stop the particle system you're resetting it to it's starting position.
    It takes 1-2 seconds for the particle system to "warm up" due to the speed of your particles.

    If you check "Prewarm" in the particle system settings, this warm-up period is skipped and the particles will always play from the warmed up point in time.

    Is that what you're looking for?
     
    Sibasis2000 and Saganomics like this.
  3. pbaumgartner98

    pbaumgartner98

    Joined:
    Mar 1, 2020
    Posts:
    13
    First of all: thank you!
    it definitley looks like it's prewarming but checking the "Prewarm" option didn't change anything.

    Here is a video of the problem. You can see that it takes some time till the particle gizmo appears after the system was stopped. If i call .Play() before the gizmo appears nothing happens.

     
  4. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    If you put some Debug.Log calls in the play part of your code, is it actually getting in there when you expect?

    What determines "isDrifting" and "driftDir"?

    If you put the particle system in the scene by itself and check play automatically, does it play as soon as the scene starts?
     
  5. pbaumgartner98

    pbaumgartner98

    Joined:
    Mar 1, 2020
    Posts:
    13

    Thanks for the reply
    I found out what the problem is
    If you call .Stop() on a particle system you can only start it with .Play() again when all particles have despawned. Since I used a lifetime of 3 seconds I couldnt start the particle system again.
    Do you by any chance know a way around this?
     
  6. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    Oh, I see what you mean. The particles are still alive so you'll need to find a way to kill them or just change the "max particles" on your particle system to a higher number.

    You can use ParticleSystem.Clear() to remove all particles.

    I'd recommend increasing the max particles though if performance is not an issue.

    Edit: There's also a "Stop and Clear" option when stopping the particle system:
    https://docs.unity3d.com/ScriptReference/ParticleSystem.Stop.html

    Code (CSharp):
    1. system.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
     
    adamgolden likes this.
  7. pbaumgartner98

    pbaumgartner98

    Joined:
    Mar 1, 2020
    Posts:
    13
    My particle count is at 1000 so that's no issue.
    Every particle has to be killed before the system can start again no matter how many you have.
    I would like to have the old particles still around when i start the system again but it seems like i have to clear first :/

    Thank you for your help!
     
  8. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
  9. IgyIgy

    IgyIgy

    Joined:
    Jan 7, 2020
    Posts:
    1
    I've been running into a similar issue and been banging my head. For some reason adding this check on calling Play caused them to start playing. I'm wondering if the isPlaying and isPaused properties aren't being properly set in some situations. You have virtually the same code but it might not hurt to give this a try. Seems silly but I didn't do this for the longest time because it "should" have worked by just calling *ParticleSystem.Play() but alas, it makes it work for me.



    Code (CSharp):
    1.             if (!particle.isPlaying)
    2.             {
    3.                 particle.Play();
    4.             }
     
    drwaldo likes this.
  10. ahmetthor123

    ahmetthor123

    Joined:
    Oct 5, 2020
    Posts:
    1
    after 6 years you answered but this helped me thank you
     
  11. B4ttleCat

    B4ttleCat

    Joined:
    Mar 31, 2014
    Posts:
    18
    azaridi likes this.
  12. drwaldo

    drwaldo

    Joined:
    Mar 17, 2021
    Posts:
    1
    This solved the issue for mi, thanks for sharing!
     
  13. Isgar

    Isgar

    Joined:
    Jun 29, 2015
    Posts:
    1
    Looks like the isPlaying method returns true while some particle are still active.
    You need to use isEmitting

    Code (CSharp):
    1.  
    2. if (!MyParticles.isEmitting)
    3. {
    4.     MyParticles.Play();
    5. }
    6.  
     
    niqle_dev and Nicolas_Granese like this.
  14. unity_4XZA4-OseWOTZA

    unity_4XZA4-OseWOTZA

    Joined:
    Dec 5, 2020
    Posts:
    1
    Cheers Isgar, this is the solution I needed!
     
  15. Sibasis2000

    Sibasis2000

    Joined:
    Feb 25, 2023
    Posts:
    2

    Thank you. It saved me from being stuck for the next few hours.
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971