Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GameObject is destroyed by which script

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())

like image 596
Manish Singh Avatar asked Dec 29 '19 07:12

Manish Singh


People also ask

How do you destroy a GameObject on collision Unity?

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.


1 Answers

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.

like image 185
TJ Michael Avatar answered Oct 06 '22 00:10

TJ Michael