C# Hot Swapping resetting static variables workaround

Hey Guys,

I have a problem when trying to work with hot swapping in my project. Here is the problem and how I have tried to solve it without any success. I have monobehavior classes that act as Managers in my game. So I have a object in the scene called “XManager” and a script XManager attached to it. When I change code, all my static variables become null and I get a bunch of null reference exceptions in my code. The only thing I can think of is that every time some one refer to my instance, I check if it is null, if so, I found my manager object in the scene and get the XManager test and set it back as the instance. I know that GameObject.Find is heavy and that is probably not a good way to solve my problem so I wonder if any of you guys found a better work around for this problem ?

Thanks a lot for your time :slight_smile:

Claude

I wouldn’t worry about hot swapping performance because it only happens in editor and only on recompile. To support static fields refer to this article Unity hotswapping notes → Supporting static fields which pretty much uses the same technique you described plus utilizing the AppDomain.CurrentDomain.DomainUnload event.