Despawn Objects

ObjectNet allows despawning of networkobject across the network.

The script does not need to be inheriting from Network Behaviour for it to work, you can spawn and despawn on any Unity C# scripits.

Despawning on the Server

On the server (or host), you can despawn network objects using Unity's standard Destroy() method. ObjectNet's built-in detection system will automatically recognize when a NetworkObject is destroyed and synchronize it across all connected clients. This makes it easy to use Unity’s native object creation workflow for networked objects.

Alternatively, you can use ObjectNet's dedicated NetworkGameObject.NetworkDestroy(); method for similar results.

// Using Unity's Instantiate method
GameObject.Destroy(this.gameObject);

// OR using ObjectNet's NetworkGameObject.Instantiate
NetworkGameObject.NetworkDestroy(this.gameObject);

Both methods will ensure the object is destroyed on all clients and properly synchronized.

Despawning on Clients

On clients, ObjectNet's automatic detection system does not apply. This means that simply using Unity’s Destroy()method will only destroy the object locally and will not destroy it on other clients.

To despawn an object across all clients from a non-server instance, you must use NetworkGameObject.NetworkDestroy();. This explicitly informs the server to handle the object creation and propagate it to all other connected clients.

// Using ObjectNet to despawn the network object on all clients
NetworkGameObject.NetworkDestroy(this.gameObject);

This ensures the object is properly destroyed across the network, regardless of whether the request originated from the server or a client.