Particle System Error

Hi

I’m super new at C# and unity, but following a class that was recorded on an old version I cannot figure out how to make the new (not new anymore) particle system to work.

I keep getting this error as soon as the object collides with something:
NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
UnityEngine.ParticleSystem+EmissionModule.set_enabled (Boolean value) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/ParticleSystem/ParticleSystemBindings.gen.cs:404)
MagicOrb.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/MagicOrb.cs:47)

This is the code:

public class MagicOrb : MonoBehaviour
{

public int hitpoint = 10;
public float speed = 5.0f;
public AudioClip audioHit = null;
public AudioClip audioShoot = null;
public ParticleSystem particle = null;
public float lifetime = 5.0f;

private bool canMove = true;

private void Awake()
{
this.GetComponent().PlayOneShot(audioShoot);
}

private void Update()
{
MoveObject();

Destroy(gameObject, lifetime);
}

void MoveObject()
{
if (canMove)
{
this.transform.Translate(0, 0, speed * Time.deltaTime);
}

}

private void OnTriggerEnter(Collider other)
{
this.GetComponent().PlayOneShot(audioHit);
this.GetComponent().enabled = false;
this.GetComponent().enabled = false;

canMove = false;

ParticleSystem.EmissionModule em = particle.emission;
em.enabled = false;

Destroy(this.gameObject, audioHit.length);
}

}

If anyone can help I would appreciate it. I’ve been stuck trying to google a solution that I could make sense of for over an hour.

You never seem to set the “particle” variable. For some reason, Unity has always had this awkward issue where it doesn’t simply say “particle is null”, but instead a unrelated problem dealing with modules.

Also a unrelated tip: I would cache GetComponent of AudioSource / Renderer / Collider / ParticleSystem in awake, then you don’t have to do it all the time in OnTriggerEnter

I don’t know how to do either of those things. Could you give me an example?

I realized that maybe you did set the particle component variable in the inspector. But just in case, here’s an example of getting the ParticleSystem component on awake, in case it hasn’t been set. And it also saves references to the attached audioSource and stuff, so you don’t need to run GetComponent() all the time (which is slow)

    public class MagicOrb : MonoBehaviour
    {

        public int hitpoint = 10;
        public float speed = 5.0f;
        public AudioClip audioHit = null;
        public AudioClip audioShoot = null;
        public float lifetime = 5.0f;

        public ParticleSystem particle;
        private AudioSource m_audioSource;
        private Renderer m_renderer;
        private Collider m_collider;

        private bool canMove = true;

        private void Awake()
        {
            //Cache
            m_audioSource = GetComponent<AudioSource>();
            m_renderer = GetComponent<Renderer>();
            m_collider = GetComponent<Collider>();

            if (particle == null)
                particle = GetComponent<ParticleSystem>();

            m_audioSource.PlayOneShot(audioShoot);
        }

        private void Update()
        {
            MoveObject();

            Destroy(gameObject, lifetime);
        }

        void MoveObject()
        {
            if (canMove)
            {
                this.transform.Translate(0, 0, speed * Time.deltaTime);
            }

        }

        private void OnTriggerEnter(Collider other)
        {
            m_audioSource.PlayOneShot(audioHit);
            m_renderer.enabled = false;
            m_collider.enabled = false;

            canMove = false;

            ParticleSystem.EmissionModule em = particle.emission;
            em.enabled = false;

            Destroy(this.gameObject, audioHit.length);
        }

    }

Thank you so much! Its finally working properly. I also noticed that when I was updating the script that the prefab it was attached to wasn’t updating with it so that may have also been part of my problem while I was trying to fix it myself.

I haven’t learned anything about caching so far, but trying to work it out on my own I’m guessing this.GetComponent tries to load the file each time, but doing the m_audioSource lines, for example, cache the file so it only loads once on awake?

1 Like

Awesome!

It doesn’t load a file, but yes, the function is slow since it has to search for that component. With this trick, we only do it at the beginning, like you said :slight_smile:

Thank you so much for the help and the bit of extra explanation! :slight_smile:

1 Like