Unity Services instances destroyed on Application.Unload

Hello,

We are using Unity as a Library and on ios when we exit back to the native app we call AnalyticsSevice.Flush(), and then Application.Unload. However I see this in the logs.

Analytics Dispatcher is already flushing.
UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[ ])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:LogWarning(Object)
Unity.Services.Analytics.Internal.Dispatcher:Flush()
Unity.Services.Analytics.AnalyticsServiceInstance:Flush()
Unity.Services.Analytics.AnalyticsServiceInstance:GameEnded()
Unity.Services.Analytics.AnalyticsContainer:OnDestroy()
Followed by
InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in

Which tells me Analytics is being destroyed. This is a problems because when we go back to the Unity as a Library instance we see this error even before we load our scene which has the code to start up Analytics with await UnityServices.InitializeAsync()
NullReferenceException: Services require a valid dependency tree to be initialized.
at Unity.Services.Core.Internal.UnityServicesInternal.InitializeServicesAsync () [0x00000] in <00000000000000000000000000000000>:0

What is the correct way to initialize and keep services around if using Unity as a Library and Application.Unload?

Hmm, sorry no help here, but I wanted to mention that I also get that exception on boot. For me it seems to happen around the time IAP is initialized.

InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in

It doesn’t seem to cause any issues with my game, but it is very annoying.

Same here, doesn’t seem to affect anything. But the error shows up upon app initialization.

InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
  at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Authentication.AuthenticationPackageInitializer.Register () [0x00000] in <00000000000000000000000000000000>:0

Uploading Crash Report
InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
  at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Core.Registration.CorePackageInitializer.Register () [0x00000] in <00000000000000000000000000000000>:0

Uploading Crash Report
InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
  at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.RemoteConfig.CorePackageInitializer.Register () [0x00000] in <00000000000000000000000000000000>:0

Uploading Crash Report
InvalidOperationException: Package registration has been locked. Make sure to register service packages in[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)].
  at Unity.Services.Core.Internal.LockedPackageRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at Unity.Services.Core.Internal.CoreRegistry.RegisterPackage[TPackage] (TPackage package) [0x00000] in <00000000000000000000000000000000>:0
  at UnityEngine.Purchasing.Registration.IapCoreInitializeCallback.Register () [0x00000] in <00000000000000000000000000000000>:0

Managed to fix the issue by doing this two things:

  • Upgrading Odin Inspector to 3.2.1 (last line of the changelog mentions a fix for this type of issue).

  • Your initial Scene must initialize Unity services, I was initializing some static classes in an empty scene and then loading a second scene that initializes Unity services. Getting rid of the first scene and doing both things at the same scene fixed it for me.

We are having the same issue using UAAL. We have the first scene initialize the UGS through [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] but still getting the same issue. We are initializing UnityIAP in the next scene.
-sbt