I'm trying to find out my gameobject is being destroyed by which script in my game.
So far i tried to print stack but details on script which is destroying the gameobject is not mentioned there
code:
Debug.Log(UnityEngine.StackTraceUtility.ExtractStackTrace())
To destroy an object on collision within the using ty software, you have to use some form of the void OnCollisionEnter method. For 2D games you need the void OnCollisionEnter2D() method and the void OnCollisionEnter() method for 3D games.
Destroy
doesn't actually "destroy" the object it's passed right away, it adds the object to a list of objects for Unity to destroy at the end of the frame. This is when when OnDestroy
is called, which is why you won't see the stack for when Destroy
was called (if you'll actually see a stack at all, I'm pretty sure native code will call this).
So that should be the end of it... right?
Well, not exactly. If you check the Order of Execution for Event Functions then there's one event function called before OnDestroy
, OnDisable
.
Lucky for us, not only is this funciton called earlier, but (at least when I last tested this on Unity 2018.4 in play mode in the editor) OnDisable
is actually called by Destroy
, so if you get the stack there, you should, hopefully be able to determine who has been destroying your monobehaviours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With