Unity ParticleSystem Play and Stop

15,752

Solution 1

In your particleAuraPlay() function you just need to check if your ParicleSystem is running or not you can achieve this by this code...

 public void particleAuraPlay(){
      if(!particleObject.isPlaying){
          particleObject.Play();
       }     
    }

Solution 2

When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing

The is a logic error.

When the particleAuraPlay() function is called, playAura is set to true.

In the Update functions,particleObject.Play(); will be called every frame since playAura is true.

You can't do this. Calling particleObject.Play(); every frame will not actually do anything as it will attempt to play and stop particles in each frame resulting to no particles at-all.

The solution is to check if playAura is true, if true, call the particleObject.Play(); function then set playAura to false so that the particleObject.Play(); function won't be called again until particleAuraPlay() is called again.

The new Update() function fixes the logic error:

void Update()
{
    if (playAura)
    {
        Debug.Log("Running");
        particleObject.Play();
        playAura = false;
    }
}
Share:
15,752
bubibu
Author by

bubibu

Updated on July 03, 2022

Comments

  • bubibu
    bubibu almost 2 years

    My particlesystem.Play() does not work. I have spent few hours on this issue and still could not figure out.

    Whenever my character hits an object, it will call the function particleAuraPlay(), and the log message "HIT" is showing which means the function is being called properly.

    When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing. Can anyone please solve my problem?

    using UnityEngine;
    using System.Collections;
    
    public class ParticleController : MonoBehaviour {
    
        private bool playAura = false;
        private ParticleSystem particleObject;
    
        void Start () {
            particleObject = GetComponent<ParticleSystem>();
            particleObject.Stop();
        }
    
    
        void Update () {
            if (playAura)
            {
                Debug.Log("Running");
                particleObject.Play();
            }
    
        }
    
        public void particleAuraPlay()
        {
            Debug.Log("HIT");
            playAura = true;
        }
    }
    
    • user1169079
      user1169079 over 7 years
      Logic seems to be correct ... Check your particlesystem object is active or no ... and check the layer on which is drawn or position ?
    • Absinthe
      Absinthe over 7 years
      @ bubibu - Are you sure the particle system is in the right place? I've had problems before where I forgot to parent it, or I used the wrong coordinate system. Also, I wouldn't call Play in Update, just call it once.
    • bubibu
      bubibu over 7 years
      Hi Thanks for response. I pretty sure it is in the right place because if I exclude the line particleObject.Stop(), the particle will start playing.
    • Kardux
      Kardux over 7 years
      Maybe you could try to disable PlayOnAwake property of your particle system. Also, if your particle effect is composed of many particle systems (more than one parented as children of particleObject), you can call particleObject.Play(true);
    • Andrea
      Andrea over 7 years
      Can you try with if (!particleObject.isPlaying) particleObject.Play(); ? My thought is that, being the execution in Update(), the animation starts from the beginning at each frame, limiting the particles from spreading and causing the overall effect to be static. It's just a try, though.
    • Programmer
      Programmer over 7 years
      @bubibu Did you try the solution?
    • bubibu
      bubibu over 7 years
      Hi I have tried out all of the solution given but none of them works.
    • bubibu
      bubibu over 7 years
      Hi guys I have solved the problem! Instead of GetComponent<ParticleSystem>, just changed it to GetComponentInChildren and everything works fine! Thanks everyone for helping!