I’d imagine if you Spawn a network object you’ll want a corresponding Despawn call before destroying it, otherwise the object won’t be de-registered from the NetworkManager.
You can combine despawning and destroying the object with Despawn(true).
How do you know that the object won’t be de-registered? I haven’t found anything in the documentation. I’m personal code Destroy seams also to “de-registered” the object from the Manager
If you want to despawn a networkObject but not destroy it on all the clients, you should use the despawn method with DontDestroyWithOwner property set to true
You’re right, I guess the NetworkManager handles it via OnDestroy. The reason I thought it didn’t pick it up was I tried it with a network object containing a NetworkList and calling Destroy on that spammed a load of errors, whereas calling Despawn(true) was fine.
Overall its just so the Netcode System can destroy the object correctly.
If you gonna Despawn a NetworkObject, the Server will firstly check if there are children on the Object that dont have the OnDestroyWithParent flag. They will be rerooted and remain in the Scene.
Afterwards the NetworkObject will be Enqued into the ReleasedNetworkObjectIds to recycle the NetworkObjectId.
Afterwards all other Clients will be messaged that this NetworObject is gonna despawn.
Also the Object will be removed correctly from all the Arrays, for example the SpawnedObjectList.
After everything is in order, the NetworkObject is gonna be destroyed.
So to summarize, the Despawn unsubscribse the NetworkObject from everything and sends a message towards all Clients in that process. And afterwards its gonna be Destroyed.
If you wanna have a closer look into the process, just open the NetworkObject code and follow all methods resulting from the Despawn method.
Ok, so you are saying that by calling destroy directly all of the above won’t happen? To be honest, I thought the same, but my code is working fine, so I thought that behind the scene when Destroy is called the network component will automatically do his stuff.
I really whish for more detailed documentation.
I agree with the documentation statement. They improved it A LOT over the last months, but there is still so much information missing.
I expect (I havent looked into the code right now) that Netcode detects if an object in the scene is missing. But most likely that will occure somwhere in the middle of the process, which can cause issues … maybe.
The Despawn method is the safe way and I would recommend to use it.
if you gonna take a look into the code base pls share the insight with us
@Max_power1965_1 Is your object dynamically spawned or statically placed in scene?
The behaviour you describe above seems to fit with dynamically spawned, where both a despawn and destroy will be synced automatically to clients when OnDestroy is called server side.
However, if static, did you test with a client connected? Client side you should only see a despawn, no destroy. A manual destroy server side will trigger a despawn server and client side, but no destroy client side (which will need to be called manually).
You should use Despawn(destroy: true) to destroy both client and server side to get the same behaviour with dynamic vs static NetworkObjects.
Hello my objects are dynamically spawned, that it’s why the simple Destroy was working. And when I use the Despawn() method, isn’t that automatically isn’t the default parameter set to true, in other words, calling Despawn() is the same as calling Despawn(destroy: true) right?