[PUN] PhotonNetwork.Destroy() or Destroy()?

Hello, I have a simple Question… let say that i use PhotonNetwork.Instantiate(); to Instantiate an object and then i want to remove it, what is better to use if i had a [PunRPC] function?

  1. to use Destroy(); because is called in all clients ([PunRPC])
    OR
  2. PhotonNetwork.Destroy(); because i have Instantiate the object with PhotonNetwork.Instantiate();

or is the exactly the same?

If you are going to let the user destroy stuff then it means you are not running a client to server model, so its ok to call rpc and then use Destroy (), but if you are running one ,then the master client (server) should call PhotonNetwork.Destroy ().

The RPC can cause more issues. If anyone can destroy the GameObject (GO) anytime, things might go wrong, too.

Consider this case:
The owner of a GO sends updates. Some other player with crappy network decides to send a RPC to destroy it. But: The package with the RPC is lost and has to be repeated. So everyone else in the room might decide to destroy the GO at about the same time, not knowing that some “destroy” RPC is already on it’s way.
Eventually, all RPCs arrive at the server. It sends the RPCs to every client in the order that it got the RPCs.

Who destroyed the GO depends on the lag of individual players. Worse, after the first destroy is executed, all others fail.
And as the RPC is not in sync with updates sent by the owner, there might even be an update, which arrived on the server after some RPC from someone else did.

It might be OK for your game, but it’s a bit messy.

Thank for your help just a quick last question, is better to use the PhotonNetwork.Destroy(); with if (PhotonNetwork.isMasterClient) in [PunRPC] or call the PhotonNetwork.Destroy(); outside the [PunRPC] if is possible?

PhotonNetwork.Destroy() gets sent to all players, so it does not require an RPC.
You could use an RPC to tell the owner it should Destroy the object.

2 Likes

Hi based on the discussion, i still could not understand what is the best practices to destroy a networked object. Please elaborate more on it. Thanks

PhotonNetwork.Destroy(netwObj).
:slight_smile:

does PhotonNetwork.Destroy uses RPC? if not what’s the underlying logic?

It does not use RPCs but it uses the same “messaging” workflow as RPCs. So it’s an event.
Typically you don’t need to know but if you’re interested, take a look around.

Sorry for reviving this dead post after years but I was wondering what do you think of this use case:

I have my map that is made up of blocks (think minecraft), and I want to destroy some of these blocks with bombs. Because I have about +200 blocks (and players can also create more), I thought I shouldn’t have 200 PhotonViews on each but instead destroy/instantiate and then announce via [PunRPC].

The thing is though, when I do it that way it doesn’t work?

These are my scripts for creating bombs or destroying blocks:

For creating a bomb: I just instantiate it, and then give it a velocity in the direction that I am looking at

[PunRPC]
    void ThrowBomb()
    {
        GameObject instBomb = Instantiate(bomb, cam.transform.position, Quaternion.identity);
        //GameObject instBomb = PhotonNetwork.Instantiate(bomb.name, cam.transform.position, Quaternion.identity);

        Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));

        Rigidbody bombRb = instBomb.GetComponent<Rigidbody>();
        bombRb.velocity += pc.rb.velocity; //add current velocity
        bombRb.angularVelocity = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10, 10f)); //give some twists and turns
        bombRb.AddForce(ray.direction * throwForce, ForceMode.Impulse);
    }

For destroying blocks (or anything really):

[PunRPC]
    void DestroyObject(GameObject obj)
    {
        Destroy(obj);
    }

BONUS QUESTION: How does one tackle particle effects with PUN? My effects just run once then destroy themselves, but I keep getting errors in the console saying that I shouldn’t destroy these objects without Photon Network. It doesn’t sound like a good idea to have 10s maybe 100s of effects with PhotonViews though…

Thanks in advanced for all the help by the way, I must’ve read dozens of your posts in these forums and they have been really helpful.

I have same question too about this, but simply is. Can we just destroy object without using rpc or photonnetwork.Destroy(). Because Photon Transform View had automatically destroy the object Destroy()

You need to use PhotonNetwork.Destroy(), unless you do Manual Instantiation (and also manual destroy).