PlayFab / PlayFabPartyUnity

38 stars 7 forks source link

SingletonMonoBehaviour needs to clear static instance when Reload Domain option is disabled #20

Closed NavidK0 closed 3 years ago

NavidK0 commented 3 years ago

Hey there!

SingletonMonoBehaviour needs to clear the static instance variable when you have the "Reload Domain" option disabled in Unity's "Enter Play Mode Settings". You can find them in Project Settings > Editor > Enter Play Mode Settings.

This is all that is needed:

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void OnBeforeSceneLoad()
{
    _instance = null;
}

If you don't do this, you get errors in the editor such as: only one PartyManager instance may exist at a time The Party DLL could not be unloaded. Please restart Unity to unload it.

This is because static variables are never unloaded from memory when Reload Domain is disabled. Unity recommends achieving this by resetting static variables with RuntimeInitialization methods.

Thanks.

See more here (What are the side effects section) https://forum.unity.com/threads/configurable-enter-play-mode.768689/

PlayFab Party Version: PlayFab Party Unity SDK 1.5.0.3-main.0 Unity version: Unity 2020.2.2f1

NavidK0 commented 3 years ago

Ahh, it seems the problem is more nuanced than just that since it only seems to happen sometimes instead of consistently...

Not sure what could be causing it, but it doesn't seem to appear in built games, only in the editor. Anyways, closing for now since the problem is not just this.