Search Unity

Destroy GameObject in RPC

Discussion in 'Multiplayer' started by Corva-Nocta, Mar 27, 2018.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    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 (csharp):
    1. [PunRPC]
    2.     void OnGather()
    3.     {
    4.         PhotonNetwork.Destroy(gameObject);
    5.     }
    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.
     
    Last edited: Mar 27, 2018
    Carterryan1990 likes this.
  2. Carterryan1990

    Carterryan1990

    Joined:
    Dec 29, 2016
    Posts:
    79
    I’m curious if you ever found a solution, I’m having the same issue.
     
  3. RickLeung

    RickLeung

    Joined:
    Aug 9, 2018
    Posts:
    4

    Answer: ( I dun think you need RPC if you are using PhotonNetwork.Destroy)

    void OnGather()
    {
    PhotonNetwork.Destroy(gameObject);
    }
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    You should ensure the call to network destroy the object only runs on the owner or the MasterClient.