Destroy gameobject in RPC

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Destroy gameobject in RPC

CorvaNocta
2018-03-27 21:22:17

Now that I have most of my code working on Photon, I am running into a pretty big problem that I know has a simple solution, I just don't know what it is. I am trying to destroy a game object (its a Tree) but I am having some trouble with my code. Here is what I have so far, this code is on the Tree object and I want it to destroy itself when I call this function:

[PunRPC]  
    void OnGather()  
    {  
        PhotonNetwork.Destroy(gameObject);  
    }

It should be destroying the Tree itself after the rest of the code runs, but for some reason it doesn't run. Instead I get the "Failed to Network-Remove" error. I have been poking around online and I can't seem to find a concrete answer that actually works, and all the ones I try don't seem to be working. Does anyone know how to properly destroy a GameObject? I need it to destroy across the network, and this code is on the Tree itself.

Comments

[Deleted User]
2018-03-29 09:04:36

Hi @CorvaNocta,

PhotonNetwork.Destroy(...) can be only used, if the object has been instantiated with PhotonNetwork.Instantiate(...). Additionally only the owner of the object can destroy the object. PhotonNetwork.Destroy(...) is also a 'network function', means that only one client (the owner in this case) have to call this function in order to process it on every client. In other words, if the owner calls PhotonNetwork.Destroy(...) on one of his objects, it gets destroyed on each connected client as well.

If you have used Manual Instantiation for this object, PhotonNetwork.Destroy(...) won't work as well. In this case you have to use something like 'Manual Destruction' which can be done for example by using a RPC or the RaiseEvent function (basically the same way Manual Instantiation is done).

Back to top