MirrorNetworking / Mirror

#1 Open Source Unity Networking Library

Home Page:https://mirror-networking.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AOI issue - NetworkClient.DestroyObject vs NetworkIdentity.OnDestroy

joshbayley opened this issue · comments

commented

We have a custom AOI implementation so it might not be a problem by default if handled appropriately there, but we have an issue where client net ids are removed from NetworkClient.spawned AFTER moving out and back into AOI very quickly. This is happening because you're using GameObject.Destroy here:

GameObject.Destroy(identity.gameObject);

but you added a fix re scene loading to NetworkIdentity.OnDestroy here:

NetworkClient.spawned.Remove(netId);

If you're very quick you can have the OnDestroy land after respawning the object and you end up with an orphaned clone.

Fix for us seems to be just using DestroyImmediate but I'm not sure if this has any implications for features we're not using.

NOTE - apologies we're not on latest so you may have fixed this by other means, I'll test on latest shortly.

All the best

Just to make sure I understood this correctly, your AOI can spawn/despawn objects rapidly, and sometimes that causes those objects to be left behind on the client?
Could you check instead of the plain spawned.Remove in NetworkIdentity, adding a check beforehand fixes this?
Something like:

                // make sure to only remove if we are the NI in spawned for netId
                if (NetworkClient.spawned.TryGetValue(netId, out NetworkIdentity id) && id == this)
                {
                    // if an identity is still in .spawned, remove it too.
                    // fixes: https://github.com/MirrorNetworking/Mirror/issues/3324
                    NetworkClient.spawned.Remove(netId);
                }

also, any chance that we can reproduce this with one of the mirror demos?