Destroy gameobject in RPC

Options
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:

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

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

  • 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).