What is destroying my GameObject
I've searched through all my code and cannot find what is causing it. I am making no calls to destroy the object itself. About 2 seconds after I move it it disappears from the hierarchy and the game. Is there any way in the debugger to break anytime an object is destroyed so that I can get a look at the call stack?
Thanks.
You could place a breakpoint in the OnDestroy of a script that is attached to the gameobject. Unfortunately the call stack will only show the OnDestroy()... So it seems your only hope is to manually go through all Destroys in your code.
Good suggestion, but you are absolutely correct. I can break there, but the callstack is useless. Unfortunately, breakingpointing all of the other destroys reveals no additional information. Are there other ways of destroying an object without calling destroy? Is there a way with the non-pro version to see if I have a huge memory leak that is causing it?
I finally tracked it down to an auto destroy flag on a trail renderer I added to an object.
Do you have a trail renderer, that has 'autodestruct' enabled? I sure did, and boy was it annoying.
Answer by Bunny83 · Jul 20, 2013 at 08:01 PM
I would simply do this:
In MonoDevelop or Visual Studio use "Find In Files" and search your project for "Destroy("
Before each occurrence insert a Debug.Log() with a meaningful text and append the name of the object it's going to destroy. Something like: Debug.Log("Player:OnTriggerEnter: " + other.name);
In addition you can add a Debug.Log() to your OnDestroy callback (also with a meaningful text)
Run your game and check your logs.
Another good suggestion, but I've already tried this. Thanks though.
Answer by RencendStudios · Feb 13, 2018 at 08:51 PM
I know this is a really old forum but after hours of pain of not being able to figure it out. I discovered that the trail renderer is the cause of this. Hopefully, none of you are having this problem after 5 years, but if you are, I can confirm this is the cause. This is also for people of the future.
The sentence:
the trail renderer is the cause of this
should be
the trail renderer can be the cause of this when autodestruct is enabled
It's 2019 and I also had a trailrenderer set to AutoDestruct causing this issue. Wow, I didn't even know that capability existed!
It's because the autodestruct checkbox. Just disable it.
Answer by yummy81 · Feb 13, 2018 at 09:52 PM
I found out, via reflection, that two classes have the autodestruct property: UnityEngine.TrailRenderer and UnityEngine.ParticleAnimator (which is obsolete).
Answer by roberto_sc · Oct 24, 2013 at 12:39 AM
If Bunny83's solution didn't work then probably it's not your object that's being directly destroyed, but some of its parents. Try to do the same, but get the transform.root.name
instead.
Answer by Equ1l1brist · Mar 30, 2021 at 07:10 PM
Had the same issue due to having a singleton script duplicated on one of my objects.
Your answer
Follow this Question
Related Questions
I can't destroy my instantiated game object 1 Answer
How can I destroy a Gameobject from a list at a specific index 1 Answer
Destroyed instance of Prefab, can't spawn it back. 1 Answer
Remove Object from Game after Collision with Player (Unity 5.2.3f1) 1 Answer
Collision detection in 2017 3d 4.2f2 0 Answers