- Home /
Destroy child with Particlesystem will cause error
Here is my ParticleSystem like this
And every child is contain ParticleSystem
But when I destroy child at runtime, it cause error
MissingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it.
I think the problem is the parent still playing ParticleSystem, and I destroy the object or ParticleSystem suddenly.
But interestingly, it only appear when I click on the particlesystem. if I don't focus on the particlesystem, it just be destroyed with no error.
But i still want to fix it.
Is there any idea to deal with the condition
You should provide us your sample code on how you manage your system. With the information you've provided, we only can assume things that you've done, which will not be very helpful to you.
I agree with you, this is a common case of poorly asked question. But I do think the result is weird and worth filing a bug ticket. @ONCT, if you have decided to file a bug report, try to reproduce the issue in a much simpler setup, so you can just export $$anonymous$$imal involving files to a unitypackage. In "help-report a bug...", remove your project folder from the "attached files" and ins$$anonymous$$d attach your unitypackage specifically prepared for reporting your issue, this way the Unity technicians will waste less time in verifying your issue and delivering a fix quicker.
Here is my simple code:
public class SelfDestroy : $$anonymous$$onoBehaviour
ParticleSystem PS;
AudioSource au;
private void Start()
{
PS = GetComponent<ParticleSystem>();
}
private void Update()
{
if (gameObject != null && PS != null)
{
if (PS.isPlaying == false && transform.childCount == 0 && PS.main.loop == false)
{
SelfBomb();
}
}
}
private void SelfBomb()
{
PS.Stop();
PS = null;
Destroy(transform.GetComponent<ParticleSystem>());
Destroy(gameObject, 0.2f);
}
It will automatically destroy it self when finishing the particlesystem.
and as i said, it cause: alt text
Thanks for your reply, and apologies for the delay in getting back.
Try implementing an objectpooler ins$$anonymous$$d of using GetComponent() and Destroy. This is not a bug afaict. You are simply removing your object from memory when it is needed. That is why the error message is "$$anonymous$$issingReferenceException: The object of type 'ParticleSystem' has been destroyed but you are still trying to access it"
Object Pooler tutorial: https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
I have tried your suggestion, but it looks like not completed deal with my dilemma.
Here is my situation: I cache a effect in the game(ex:fireball), and when i cast the spell, it instantiate the effect at once, I think it is not okay that when every effect had play done, move it to objectpooler, that will stock many used effect, so i need to destroy it sometime, and the error appeared at all.
Anyway, it doesn't cause any bug or anything to the game, I think i can shelve it temporarily.
and thanks for your answer.
There's a delay on destroying the object, but is there a script on that object trying to reference that (now destroyed) particle system anywhere else? Where is that error co$$anonymous$$g from?
I tried to cancel the the delay, now it is simple: Destroy(transform.GetComponent()); Destroy(gameObject);
It still happened.
and here is the error log.
Answer by mr0menphis · May 26, 2017 at 12:56 PM
you destroy the children and try to play the particle system after, so basicly you try to play something, that has been destroyed. I am new to unity, but it looks like that to me, the same problem would occur in the TANKS! tutorial, they are handling it there. go to the TANKS! tutorial and check the 5th or 6th lesson, they handle that problem there, by unchilding the object and giving the Destroy() function the lifetime of the particles as delay parameter hope i could help :)