- Home /
Get a stack trace to find unknown caller
I was asked a question recently, and couldn't find a suitable answer for a long time: Say, you have a game, everything is ok, but suddenly your Player object is destroyed, and you have no idea who did this, maybe it's some library you imported. How to find the destroyer?
Or other case: you have a value field which must be positive at all times, but suddenly it becomes "-1", how to find what changed it?
So I assumed it should be something to get a StackTrace. The problem is, if I try to implement
void OnDestroy()
{
Debug.Log(StackTraceUtility.ExtractStackTrace());
}
It only works if the object is destroyed with DestroyImmediate call from other class, which is rare to use. And this doesn't really fit to the case with the field, but the answer should be common for both cases. So this is not an option. Maybe there is any way to use reflection to work this out, but I haven't figure it out yet.