Particles Not Playing

Hello!

I am making some code to play an explosion when an object collides.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Timer : MonoBehaviour
{
    public ParticleSystem explosion;

    void Start()
    {
        explosion.GetComponent<ParticleSystem>();
    }

    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Obstacle")
        {
            Time.timeScale = 0.3f;
            StartCoroutine(SloMo());
        }    
    }

    IEnumerator SloMo()
    {
        explosion.Play();
        yield return new WaitForSeconds(1);
        gameOver.SetActive(true);
        takingAway = false;
        Time.timeScale = 0f;
    }
}

The explosion doesn’t play but the game does go into slow motion like it is supposed to. The code stops and tries to play the particle but doesn’t and pauses the code and everything after the explosion.Play();
doesn’t run.

Doenst Time.timeScale = 0f pause the game?

From a google search;

“The most convenient method for pausing the game in Unity is by setting the game’s time scale to zero (Time.timeScale = 0), which effectively pauses all time based operations including movement, physics and animation. Setting the time scale to one again will return the game to its normal speed”

Yes it does - I play the particle and then wait 1 second and then show a game over popup which pauses the game behind it. The particle doesn’t play though and it doesn’t even get to that line of code. I assume the code is trying to play the particle and is waiting for the particle to play but it isn’t, thus stopping the script from playing

Are you seeing any errors in the console when you play?

Something tells me you’re probably getting a Null Reference error due to the “explosion” particle system not being properly referenced.

I see you have “explosion” as a public ParticleSystem reference, for example, yet you’re also attempting to acquire the reference in Start (which seems redundant; since it’s a public reference, why not just drag-and-drop it in the Inspector?). I strongly suspect that’s what’s causing things to break.

Try getting rid of the Start method all-together and just dragging-and-dropping your “explosion” reference in the Inspector.

1 Like

Thanks! I did what you said and it worked. I got all mixed up using bits from many different codes i had found with the same issue. I removed a component attached to the particle to destroy once it is played as it automatically played at the beggining and then destroyed itself

I was just wondering aswell how would I change the position of the particle to a position of a game object?

ParticleSystems (particlesystems): emitting in various positions in space (no play / stop):