How can other players destroy room objects?

MMSix
MMSix

I am having a problem getting other players that are not the master to destroy enemies. These enemies are instantiated as room objects by the master client. The master client can destroy these enemies when they shoot them with a projectile or when the enemy collides with the master client. When the other players shoot the enemy or the enemy collides with them, the enemy is not destroyed. The player is damaged however. I have tried using photonView.IsMine as well as PhotonNetwork.IsMasterClient. Any ideas?

Here is the code snippet of the collision detection where the enemy should be deleted. This script is attached to the enemy. Debug logs are called, but enemy is not destroyed when other players collide with or shoot the enemy.



Update: The collision is happening on the client side so the client can not destroy the room objects. A simple RPC call to only the Master Client, that will destroy this game object, fixed the problem! Idk if there is a better wat, but a RPC call worked, for anyone else with this problem.

Answers

  • What about PhotonNetwork.Destroy(gameobject)

  • GetGoodIn
    GetGoodIn
    edited December 2021

    I legit spend way too long on this. Wish there was a straight to the point tut on destroying stuff and the most common stuff. But for people who need this, what I did was:

    I used a Raycast when pressing E - i did:

          if (hit.collider.gameObject.tag == "Item")

        {

            int viewID = hit.collider.GetComponent<PhotonView>().ViewID;

           photonView.RPC("DestroyFoodItem", RpcTarget.MasterClient, viewID);

          }


    And then the Function:

      [PunRPC]

      public void DestroyFoodItem(int viewID)

      {

        PhotonNetwork.Destroy(PhotonView.Find(viewID).gameObject);

      }

    And it finally worked, without errors. Client can destroy gameobjects and so can the master.

  • GetGoodln, thank for your reply. I did exactly the same thing but still only master can destroy the objects... :(