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

Particle system destroyed when ParticleSystem.Play() is called

Discussion in '2D' started by ajlott, Dec 7, 2019.

Thread Status:
Not open for further replies.
  1. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    I have a particle system that I want to play in code. However, when I call ParticleSystem.Play(), I get a NullReferenceException saying the particle system has been destroyed but I am still trying to access it. Removing the ParticleSystem.Play() call removes the error, so I know that's the issue. The question is, why on God's green earth would that happen? I'm assuming it has something to do with the collision, because ParticleSystem.Play() has worked for me very recently in other contexts, such as when a button is pressed. Any help is appreciated.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerDie : MonoBehaviour
    7. {
    8.     public ParticleSystem blood;
    9.  
    10.     public void OnCollisionEnter2D(Collision2D collision)
    11.     {
    12.         blood.Play();
    13.     }
    14. }
    15.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @ajlott,

    Have you assigned a particle system to that public field you have in your Inspector? If it's assigned, are you sure that you're not destroying the object where that script is assigned to? As it's name is PlayerDie, I thought you might destroy player (just a guess.)

    At least add a check there to avoid errors and do some debug log prints to see what's exactly happening.

    Code (CSharp):
    1. if (blood != null) {
    2.     blood.Play();
    3. }
    That OnCollisionEnter(2D) doesn't really differ that much from any other method, it's just called by Unity when a collision occurs to that object where that script is assigned to.
     
  3. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    Thank you for the reply. Yes, there is a particle system assigned to the public field and I am not destroying the object the script is attached to. I will eventually destroy the object, but I wanted to get the particle system to play first. Also, if I comment out the ParticleSystem.Play call, there's no error when the collision occurs.

    Thanks again for the reply!
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You probably need to drag the particle system into the field in the inspector field.
     
  5. nelson_unity888

    nelson_unity888

    Joined:
    Jun 19, 2021
    Posts:
    2
    In my case the problem was a particular script associated with the explosion I was using. I was using a freely available explosion that came with a script that destroyed the particle system when it was done.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please don't necro threads like this. Also, the 2D forum isn't actually about particles.
     
Thread Status:
Not open for further replies.