Issues Deleting all PlayerObjects at once, Works on some clients and not others

I’m curious if anyone can help me diagnose the issue here or whether it’s a bug.

When iterating through all connected clients, retrieving their PlayerObjects and then attempting a Despawn & Destroy, not all of the clients reflect what is called.

I notice that order of connection determines which clients destroy all, and which do not.

I’ve checked with 1 host and up to 4 other clients, and in each case the only clients that reflect all PlayerObjects being destroyed are the first connected client(the host) and the last connected client. All clients that connected between these two, will have leftover PlayerObjects that failed to get deleted.

To name a few I have attempted many methods of deletion in the loop, such as a simple Despawn(true)/ Despawn/Destroy on the host. I’ve also tried doing the deletion via ClientRpc, to which I have the same outcome. I’ve tried adding a delay, and I’ve even run some tests sending values instead of destroying and each client is aware of the IDs being passed around

It’s been driving me quite mad so I’m curious if it’s just a bug I’m experiencing.

This was my initial code , all other methods produce the same as this.

if (IsServer)
    {
        
        foreach (var client in NetworkManager.Singleton.ConnectedClients)
        {
            
            GameObject lobbyObject = client.Value.PlayerObject.gameObject;

            if (lobbyObject != null)
            {
                NetworkObject netObj = lobbyObject.GetComponent<NetworkObject>();
                if (netObj != null && netObj.IsSpawned)
                {
                    netObj.Despawn();  

                    Destroy(lobbyObject);  

                }
            }
        }
    }

Thank you for your help!

For anyone else who ends up with an issue like me, I was able to solve the issue by changing the ownership to the host before mass deleting.

1 Like

Additionally, if you call netObj.Despawn(true), it will handle destroying the object on all clients after despawning automatically. Apparently doing it via Despawn and then Destroy can cause inconsistent behaviour on clients when despawning static pre-placed objects, so Despawn(true) is the recommended method.