- Home /
How can I debug gameObject destruction?
I have a gameObject that is being destroyed for an unknown reason. How can I determine who or what is causing my object to be destroyed? Getting a callstack from OnDestroy or OnDisable doesn't help, because of delayed destruction.
Edit: In fact, a breakpoint (or log message) in OnDisable for the destroyed object DOES point me to the offender in this particular case; another script is calling Destroy which calls OnDisable in the same callstack. I'm not sure if this is true for other causes of destruction.
I seem to recall a similar question here a day or two ago. He had licenced or otherwise proprietary code he couldn't show. Are you using any 3rd party code which exhibits the behaviour? I'd like to know what this code is and where to get it for myself.
Answer by ryanbailey · Jul 22, 2013 at 10:47 PM
The only thing I could think of is to check all scripts in the scene for the Destroy method. Also check any turned off scripts for an auto-destruct boolean. I know in the past I had a LineRenderer turned off but it was still destroying a stagnant game object because auto-destruct was on.
Good Luck!
-Ryan Bailey
Answer by IgorAherne · Jul 22, 2013 at 10:15 PM
Hm, interesting question. You probably tried this, but
go through scripts and next to every destroy method put Debug.Log("Destruction Number 1 , 2 etc"); If the code is big, just CTRL+F and search for Destroy
the very moment the object is killed - pause the game and check the console
if you don't have any destroy calls, than it's not your scripts that are destroying the object, but the program perhaps?
Thanks for the reply, Igor. We do have a fairly large codebase and some fairly generic destruction code, so this method is cumbersome and doesn't get us full coverage (destruction due to level loading, destroyed transform parents, garbage collection, etc.). I really need some way to trap the destruction when it gets queued up (for any reason) for a particular object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110632im_/https://answers.unity.com/themes/thub/images/avi.jpg)