Search Unity

Resolved How to remove gameobject from client and sync

Discussion in 'Netcode for GameObjects' started by MiTschMR, Jan 19, 2022.

  1. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    487
    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:
    Code (CSharp):
    1. protected override Task Run(Args args)
    2.         {
    3.             NetworkObject networkObject = this.m_GameObject.Get(args).GetComponent<NetworkObject>();
    4.  
    5.             NetworkManager.Singleton.GetComponent<NetworkGameObjectHandling>().HandleGameObjects(GameObjectOperation.Destroy, networkObject.NetworkObjectId);
    6.            
    7.             return DefaultResult;
    8.         }
    The two methods in the NetworkGameObjectHandling script that is on the NetworkManager gameobject which should tell the server to destroy the gameobject:
    Code (CSharp):
    1. public void HandleGameObjects(GameObjectOperation operation, ulong networkId)
    2.         {
    3.             switch (operation)
    4.             {
    5.                 case GameObjectOperation.Instantiate:
    6.                     break;
    7.                 case GameObjectOperation.Destroy:
    8.                     UnityEngine.Debug.Log(UnityEngine.GameObject.FindObjectsOfType<NetworkObject>().Where(n => n.NetworkObjectId == networkId).FirstOrDefault().IsOwnedByServer);
    9.                     DespawnGameObjectServerRpc(networkId);
    10.                     break;
    11.             }
    12.            
    13.         }
    14.  
    15.         [ServerRpc]
    16.         public void DespawnGameObjectServerRpc(ulong networkId)
    17.         {
    18.             NetworkObject networkObject = UnityEngine.GameObject.FindObjectsOfType<NetworkObject>().Where(n => n.NetworkObjectId == networkId).FirstOrDefault();
    19.             UnityEngine.Debug.Log(networkObject.NetworkManager);
    20.             if (networkObject != null) Destroy(networkObject);
    21.         }
    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 ;)
     
  2. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    146
  3. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    487
    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?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    Try [ServerRpc(RequireOwnership = false)]
     
    MiTschMR likes this.
  5. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    487
    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!
     
  6. myeagle17

    myeagle17

    Joined:
    Jul 24, 2014
    Posts:
    2
    Same as you.It doesn't remove in the server , is it a bug?
     
  7. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    487
    I hope it is not. But you can file a bug report so it gets investigated.
     
  8. dcmrobin

    dcmrobin

    Joined:
    Feb 15, 2021
    Posts:
    39
    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.
     
  9. dcmrobin

    dcmrobin

    Joined:
    Feb 15, 2021
    Posts:
    39
    I just fixed it by instead of having a [ClientRpc] I did [ServerRpc(RequireOwnership = false)] and that fixed it