Variables set during OnPostProcessAllAssets inside containing class are reset by the time the DidReloadCallbacks callback is reached.

Hey Everyone!

I’ve been having some headaches with Unity recently, mainly in related to changing player defines based on the importing/removing of components from the project. I’ve narrowed my issue down to where I’m setting static members to be a certain value in the OnPostprocessAllAssets() function, but then when that script reaches the function called by the [UnityEditor.Callbacks.DidReloadScripts] function, that varliable has been reset.

For some very basic code examples:

public class MyClass : AssetPostprocessor
{
    static mybool = false;

    static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) 
    {
        //this is always called before OnScriptsReloaded()
        mybool = true;
    }

    [UnityEditor.Callbacks.DidReloadScripts]
    static void OnScriptsReloaded()
    {
        if(myBool) { 
            //this code is never reached, myBool always is false here
        }
    }
}

If anyone has any insight here on what I’m doing wrong, I’d love to hear it! Ideally, the myBool var will maintain its value across both functions.

Hi @massivebacon! The problem is that static variables will be reset when the domain reloads. The only fields that survive a domain reload are serialized/serializable fields on a UnityEngine.Object. (See this page in the documentation.)

In this case I would recommend using something like SessionState.GetBool () and SessionState.SetBool (). I just realized these are unfortunately not documented, so I will try to get that fixed, but they basically let you store a key-value pair that exists as long as your editor session is active