GameObject Disappears after script runs

Please Help ;(
I have a my gun with a child object called ‘Gun tip’. I added a particle system to the Gun tip and the script to run the particles in the gun. When I play my project i can shoot once -the particle system works fine- but the ‘Gun tip’ Object gets destroyed. I got an error saying that the GameObject Particle system was destroyed but the script is trying to access it :

Please answer me

also I’m running Unity 2019.4.40f1

Do you destroy the particle system at any point?

Sounds like the particle system is destroying itself, check the Stop Action field in the Main Module: Unity - Manual: Particle System Main module

No i didn’t
and to ensure it wasn’t in the code i copy pasted :slight_smile:
i checked it and nothing contained destroy();

thx for ur answer but I’ve tried this before. But i have used the following code to disable / enable the particle system when left clicked :

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

public class Particlesscript : MonoBehaviour
{
    public GameObject gun;
    private ParticleSystem Particles;
    // Start is called before the first frame update
    void Start()
    {
        Particles = gun.GetComponent<ParticleSystem>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) {
            Particles.Play();
        }
        else
        {
            Particles.Stop();
        }
    }
}

Maybe .Stop(); is a wrong command?

Check your scene view to make sure that the particle system is still there when the error is present.

The Stop function should be perfectly fine for your application, though, do look into the different overloads of the function too, just in case.

The error that you’re getting can only be obtained if the object that you are trying to reference, I.E. ParticleSystem in your case, gets destroyed.

In this case it does not just mean the GameObject that the particle system is attached to but also the ParticleSystem component on the GameObject in question.

This error can also present if any parents of the ParticleSystem’s GameObject are destroyed, so keep that in mind too.

The error presents when the particle system gets deleted with the Gun Tip

Sounds about right. You should destroy the Particlescript alongside the Gun Tip, so that it doesn’t try to access the particle system when it doesn’t exist. Alternatively (but not preferred), just check that the particle system exists before trying to use it.

The get the error just when the Gun tip get destroyed
(the gun tip has the particle system)

Lets understand the error a little more:

MissingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it.

The first important part is the MissingReferenceException, it tells us that we are referring to something that is missing.
The second important part is the type, it tells us that an object of the type ParticleSystem is the culprit
The third part is that said object has been destroyed
And the final, most important part, is that we’re still trying to access it even though it doesn’t exist anymore.

How can we prevent this?
Well we just need to stop trying to access the particle system if it doesn’t exist anymore.

Imagine you’re talking to someone and they just walk away with no warning, you’d stop talking right? Well right now you’re continuing the conversation even though they aren’t there anymore.

Thank you, i understand what u mean but i dont even want the particle system to get destroyed. I need particles for my gun cuz my game is boring.

In that case, you need to move the ParticleSystem off of your gun tip.

i’ll try that rn thx

… so did you check what the Stop Action is set to?

Yea 8383659--1105614--upload_2022-8-23_16-35-38.png

Um yea i found that the particle system is DESTROYING ITS SELF :frowning:

Thanks u i fixed it :smile:

Thank u i fixed it ;D