@@ -109,6 +109,12 @@ public static IEnumerable<string> ActiveClientUlids
109109 /// </summary>
110110 public static LootLockerPresenceManager Get ( )
111111 {
112+ // During Unity shutdown, don't create new instances
113+ if ( ! Application . isPlaying )
114+ {
115+ return _instance ;
116+ }
117+
112118 if ( _instance != null )
113119 {
114120 return _instance ;
@@ -173,7 +179,10 @@ public void SetEventSystem(LootLockerEventSystem eventSystemInstance)
173179
174180 void ILootLockerService . Reset ( )
175181 {
176- _DestroyAllClients ( ) ;
182+ if ( ! _isShuttingDown )
183+ {
184+ _DestroyAllClients ( ) ;
185+ }
177186
178187 _UnsubscribeFromEvents ( ) ;
179188
@@ -221,11 +230,14 @@ void ILootLockerService.HandleApplicationFocus(bool hasFocus)
221230
222231 void ILootLockerService . HandleApplicationQuit ( )
223232 {
224- _isShuttingDown = true ;
225-
226- _UnsubscribeFromEvents ( ) ;
227- _DestroyAllClients ( ) ;
228- _connectedSessions ? . Clear ( ) ;
233+ if ( ! _isShuttingDown )
234+ {
235+ _isShuttingDown = true ;
236+
237+ _UnsubscribeFromEvents ( ) ;
238+ _DestroyAllClients ( ) ;
239+ _connectedSessions ? . Clear ( ) ;
240+ }
229241 }
230242
231243 #endregion
@@ -814,6 +826,12 @@ public static bool IsPresenceConnected(string playerUlid = null)
814826 /// </summary>
815827 public static LootLockerPresenceConnectionStats GetPresenceConnectionStats ( string playerUlid = null )
816828 {
829+ // Return empty stats during shutdown to prevent service access
830+ if ( ! Application . isPlaying )
831+ {
832+ return new LootLockerPresenceConnectionStats ( ) ;
833+ }
834+
817835 var instance = Get ( ) ;
818836 if ( instance == null ) return new LootLockerPresenceConnectionStats ( ) ;
819837
@@ -953,7 +971,14 @@ private void _DestroyAllClients()
953971 _connectingClients . Clear ( ) ;
954972 }
955973
956- // Destroy all clients outside the lock
974+ // During Unity shutdown, don't destroy objects manually to avoid conflicts with LifecycleManager
975+ if ( ! Application . isPlaying || _isShuttingDown )
976+ {
977+ // Just clear the collections, let Unity handle object destruction during shutdown
978+ return ;
979+ }
980+
981+ // Destroy all clients outside the lock (only during normal operation)
957982 foreach ( var client in clientsToDestroy )
958983 {
959984 if ( client != null )
@@ -1353,14 +1378,22 @@ private LootLockerPresenceClient _GetPresenceClientForUlid(string playerUlid)
13531378
13541379 private void OnDestroy ( )
13551380 {
1381+ // During Unity shutdown, avoid any complex operations
1382+ if ( ! Application . isPlaying )
1383+ {
1384+ return ;
1385+ }
1386+
13561387 if ( ! _isShuttingDown )
13571388 {
13581389 _isShuttingDown = true ;
13591390 _UnsubscribeFromEvents ( ) ;
13601391
1392+ // Only destroy clients if we're not in Unity shutdown
13611393 _DestroyAllClients ( ) ;
13621394 }
13631395
1396+ // Skip lifecycle manager operations during shutdown
13641397 if ( ! LootLockerLifecycleManager . IsReady ) return ;
13651398
13661399 // Only unregister if the LifecycleManager exists and we're actually registered
0 commit comments