How to destroy an Object on all the Network ?

Hi,

I have a Host and some Clients connected to this Host.
I have a GameObject with a Network Identity (it’s a button).

Whenever I click this button, I want it to be removed from the game (on all Clients, Host included), if either the client or the host clicks on it.

I am doing the following on the button click event:

NetworkServer.Destroy(GameObject.Find("Button"));

It’s working fine when the Host clicks on the button (The button gets destroyed on all Clients), but not the other way around :frowning: → When any client Destroys the Button, it is only destroyed locally (The Button does not have a Client Authority).

Any help would be appreciated.
Thanks !

I believe that a client must also pass a viewID. Networking isn’t my strong suit but I believe that clients can not be delete objects themselves; rather a client should pass a message to the server/host about which object to destroy.

objectToBeDestroyed = GameObject.Find ("Button");
Network.Destroy(objectToBeDestroyed.GetComponent<NetworkView>().viewID);

Create a method that has a command attribute so it is executed on the host:
https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html

[Command]
public void CmdDestroyButton(GameObject button) {
	NetworkServer.Destroy(button);
}

Then call CmdDestroyButton(GameObject.Find(“Button”)) on click.

Consider using NetworkIdentity.AssignClientAuthority for this. Keep in mind the object to be destroyed must have LocalPlayerAuthority set to True in the NetworkIdentity Component for the ownership transfer to be successful, and this is required for one client to delete another clients object.
https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.AssignClientAuthority.html

The code may not be the best, but here is an example:

Usage:
NetworkDestroy(button.gameObject);

using UnityEngine;
using UnityEngine.Networking;

public class Example : NetworkBehaviour
{
    void NetworkDestroy(GameObject Object)
    {
        //Get the NetworkIdentity assigned to the object
        NetworkIdentity id = Object.GetComponent<NetworkIdentity>();
        // Check if we successfully got the NetworkIdentity Component from our object, if not we return(essentially do nothing).
        if (id == null) return;
        // First check if the objects NetworkIdentity can be transferred, or if it is server only.
        if (id.localPlayerAuthority == true) {
            // Do we already own this NetworkIdentity? If so, don't do anything.
            if (id.hasAuthority == false)  {
                // If we do not already have authority over the NetworkIdentity, assign authority.
                // Keep in mind, using connectionToClient to get this NetworkIdentity is only valid for Network Player Objects.
                if (id.AssignClientAuthority(connectionToClient) == true)
                {
                    // If takeover was successful, we can now destroy our GameObject.
                    Network.Destroy(Object);
                }
            }  else {
                // Do nothing because we already have ownership of this NetworkIdentity.
            }
        } else {
            //Server only, so we can't do anything.
        }
    }
}

Ok so I finally managed to get it working:
To fix it, I used a Command function, but I had to put this function in a Player Script.
If the script is not on the player, that doesn’t work… (it actually seems impossible to send Commands outside Player objects)

Thanks for the help everyone

I have gone through your past conversation and I have followed the recommended steps. I still have problem with client to server sync/update. I used [Command] to send the destroy command to the server, in player’s script and also checked LPA on the GO’s Network Identity, to be destroyed, but still no lack!

Here is my scenario…

In a MatchMaker multiplayer game (Unity version 2018.1.0f2), when a GO is destroyed on the host, that GO is also destroyed on the client(s) but the other way round cannot be accomplished, here is my script code below, attached to the GO that is needed to be destroyed (the GO has “Server Only” and “Local Player Authority” options unchecked and “Network Transform Send Rate” to 9):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class GameObjectDestroyScript : NetworkBehaviour
{

[SerializeField]
GameObject GOobject;

private void OnMouseDown()
{
Destroy(GOobject);
}

}

I need a GO to be destroyed by the client(s) and be sync/updated to the server/host, in order to subsequently sync/update the clients’ GO!

Please ask for any feedback/info that you may need in order to help me.

Thank you for your time!