Home > Mobile >  How to find the script that destroys my object?
How to find the script that destroys my object?

Time:01-11

I work on a physics-heavy project with a lot of rigid-bodies which is also code-heavy. I have a lot of scripts, that create forces, manage joints and so on.

For some reason, sometimes a randomly a certain Object is destroyed. It just disappears and is gone. I can't figure out which script causes that. Is there a way to find out which script called Ondestroy or something.

Thanks for you help.

CodePudding user response:

Put a brakpoint in the OnDestroy() method of your monobehaviour and check the call stack.

To debug your project you can check Debugging C# code in Unity. Once you figure that out and check how to attack unity and stop execution at the set breakpoints you can do with Window->Debug->Callstack to see the call stack with the execution stopped at your breakpoint and see what is being called that leads to your object destruction.

CodePudding user response:

Simply have a component like e.g.

public class DestroyDebug : MonoBehaviour 
{
    void OnDestroy ()
    { 
        Debug.Log($"{name} was just destroyed");
    }
}

Either put a breakpoint there while Debugging your code if you need the exact instances and circumstances or also in the console you already can at least see the entire stacktrace of which classes and calls exactly led to destroy of this object.

  •  Tags:  
  • Related