Particle system keeps playing even when Looping is off?

Sorry for the noob-ish question but this is my first time using particle systems. I created a particle system and added a script to it. In the script, the particle system has a reference to a game object. When the game object is destroyed, I want the particle system to play for 0.5 seconds and then stop playing. I set all the required values in the particle system inspector. Duration is 0.5 and Looping is unchecked. When I simulate it in the scene view, it works perfectly. It only plays for 0.5 seconds and then stops. So nothing wrong there. But when I play it in the game view, the particle system keeps looping after the game object is destroyed. Here’s the script that I added to the particle system:

    GameObject theGameObject;
	ParticleSystem particleSystem;

	void Start () {

		theGameObject = GameObject.FindGameObjectWithTag ("Player");
		particleSystem = GetComponent<ParticleSystem> ();
	}
	
	void Update () 
	{

		if(theGameObject != null)
		{
			//event irrelevant to this question
           transform.position = theGameObject.transform.position;
		}
		else
		{
			particleSystem.Play();

		}

	} 

How can I change the script so that the particle system plays only once after the game object is destroyed?

Since inside your update once the object is destroyed theGameObject becomes null and else condition runs continuously.

You can set and check a bool to play only one as below:

GameObject theGameObject;
ParticleSystem particleSystem;
bool particleSystemPlayed = false;
 
 
	void Start () {
 
		theGameObject = GameObject.FindGameObjectWithTag ("Player");
		particleSystem = GetComponent<ParticleSystem> ();
    }
     
    void Update () 
    {
 
        if(theGameObject != null)
        {
            //event irrelevant to this question
           transform.position = theGameObject.transform.position;
        }
        else
        {
			if(!particleSystemPlayed)
			{
				particleSystem.Play();
				particleSystemPlayed = true;
			}
        }
    }

Go to your particle system
Stop Action > Choose “Destroy”

it’ll destroy the object once its finished playing…