Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Multiplayer' started by p627, Jul 28, 2015.

  1. p627

    p627

    Joined:
    Apr 12, 2014
    Posts:
    23
    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?
     
  2. Crystalline

    Crystalline

    Joined:
    Sep 11, 2013
    Posts:
    159
    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 ().
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    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.
     
  4. p627

    p627

    Joined:
    Apr 12, 2014
    Posts:
    23
    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?
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    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.
     
    Westland and ixabhay like this.
  6. limtasheng

    limtasheng

    Joined:
    Mar 21, 2020
    Posts:
    2
    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
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    PhotonNetwork.Destroy(netwObj).
    :)
     
  8. limtasheng

    limtasheng

    Joined:
    Mar 21, 2020
    Posts:
    2
    does PhotonNetwork.Destroy uses RPC? if not what's the underlying logic?
     
  9. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,013
    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.
     
  10. Ziberian

    Ziberian

    Joined:
    Jan 9, 2018
    Posts:
    64
    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
    Code (CSharp):
    1. [PunRPC]
    2.     void ThrowBomb()
    3.     {
    4.         GameObject instBomb = Instantiate(bomb, cam.transform.position, Quaternion.identity);
    5.         //GameObject instBomb = PhotonNetwork.Instantiate(bomb.name, cam.transform.position, Quaternion.identity);
    6.  
    7.         Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    8.  
    9.         Rigidbody bombRb = instBomb.GetComponent<Rigidbody>();
    10.         bombRb.velocity += pc.rb.velocity; //add current velocity
    11.         bombRb.angularVelocity = new Vector3(Random.Range(-10f, 10f), Random.Range(-10f, 10f), Random.Range(-10, 10f)); //give some twists and turns
    12.         bombRb.AddForce(ray.direction * throwForce, ForceMode.Impulse);
    13.     }
    For destroying blocks (or anything really):
    Code (CSharp):
    1. [PunRPC]
    2.     void DestroyObject(GameObject obj)
    3.     {
    4.         Destroy(obj);
    5.     }
    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.