How to remove gameobject from client and sync

Hey all

Pretty new to Netcode, so please have patience with me understanding it.

I have a test scene with gameobjects, that when pressing E, get destroyed. Of course this works great when not using Netcode. Now, I want that if a client destroys one of these gameobjects, it is synced to the server and from there to the other clients. The gameobjects already exist in the scene upon spawning the players, meaning they are server owned. They all have a NetworkObject component.

My code looks like this:
Method of script that is called to invoke the destroy:

protected override Task Run(Args args)
        {
            NetworkObject networkObject = this.m_GameObject.Get(args).GetComponent<NetworkObject>();

            NetworkManager.Singleton.GetComponent<NetworkGameObjectHandling>().HandleGameObjects(GameObjectOperation.Destroy, networkObject.NetworkObjectId);
           
            return DefaultResult;
        }

The two methods in the NetworkGameObjectHandling script that is on the NetworkManager gameobject which should tell the server to destroy the gameobject:

public void HandleGameObjects(GameObjectOperation operation, ulong networkId)
        {
            switch (operation)
            {
                case GameObjectOperation.Instantiate:
                    break;
                case GameObjectOperation.Destroy:
                    UnityEngine.Debug.Log(UnityEngine.GameObject.FindObjectsOfType<NetworkObject>().Where(n => n.NetworkObjectId == networkId).FirstOrDefault().IsOwnedByServer);
                    DespawnGameObjectServerRpc(networkId);
                    break;
            }
           
        }

        [ServerRpc]
        public void DespawnGameObjectServerRpc(ulong networkId)
        {
            NetworkObject networkObject = UnityEngine.GameObject.FindObjectsOfType<NetworkObject>().Where(n => n.NetworkObjectId == networkId).FirstOrDefault();
            UnityEngine.Debug.Log(networkObject.NetworkManager);
            if (networkObject != null) Destroy(networkObject);
        }

I get the error message “Only the owner can invoke a ServerRpc that requires ownership!”, meaning I am doing something terribly wrong, I am sure.

If someone can enlighten me with his knowledge please, I am truly grateful if I knew how to do this :wink:

Hi, I believe you have to give ownership of the object being deleted to that client.
NetworkObject | Unity Multiplayer Networking (unity3d.com)

Thanks for the reply, I have just tried it, but it gives me the same error message. Do I have to set the ownership from a different method or try a completely different approach? Maybe it is just not possible?

Try [ServerRpc(RequireOwnership = false)]

1 Like

In the server window it doesn’t remove the gameobject, but it is synced across the clients, so I would say the issue is “solved”. Thank you very much for the quick help!

Same as you.It doesn’t remove in the server , is it a bug?

I hope it is not. But you can file a bug report so it gets investigated.

So how would I have it so that when Player A shoots something enough times, on both screens (both Player A and Player B) whatever Player A shot disappears? Because right now I have it only disappearing on Player A’s screen. This probably has something to do with ownership transfer… but the object that is supposed to get destroyed doesn’t have a networkObject component on it, but its parent does.

I just fixed it by instead of having a [ClientRpc] I did [ServerRpc(RequireOwnership = false)] and that fixed it