I’m finding that in some situations I’m ending up with a ‘this’ pointer in a MonoBehavior non-static method that is null. I have actually gone into the debugger to verify that the pointer is actually null.
I was under the impression that it wasn’t even possible to have a null ‘this’ in a non-static method, so I’m not even sure how to go about debugging this issue. Any tips or guesses as to what might be going on or how this is even possible?
I managed to figure this one out myself. I was using C#'s Event system and it appears that I had some event handlers or something hanging around. I found out it only happened if I started a scene, loaded a different scene, and then came back to the first one. The event handlers were firing on an object that didn’t exist anymore.
Luckily the fix was very simple. I just needed to deregister each event handler in the OnDestroy method of each class that registered handlers.