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 :
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();
}
}
}
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.
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.
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.