SetActive(false) object for all players in room

Options

Hi,

I have a prefab "A", when i touch him i first instantiate it (the prefab has photon-view and Photon-Rigidbody-View.

Instantiation part:

 GameObject "B" =  PhotonNetwork.Instantiate(A, new Vector3(0, 0, 0), Quaternion.Euler(new Vector3(0, 0, 0))).

The problem is when i am trying to SetActive object "B".

For me the object is disabled but for everybody else it is not.

How can I set active an object for all players in room including I ?

Answers

  • Tobias
    Options

    Everyone else is not running this exact code. They instantiate the object because they received a message to do so (to replicate what the one client did). Calling SetActive(false) is not synchronized (not even on a networked object).

    There is a IPunInstantiateMagicCallback interface, which you can implement and put that script on the prefab. Whatever you want to do to it initially, do it in there and all the instances will run this code.

    Again, keep in mind that only one client / player is actually controlling the object.

  • mesmes
    Options

    But after creating "B" from "A" using the PhotonNetwork.Instantiate(.....) I want to change "B" scale / Material.

    1) I tried changing the scaling with [PunRPC] RpcTarget.AllBufferedViaServer.... for all users including me.

    2) When i do try to scale it, It is is making a "new B" in the new Scale I wanted but also keeping the Original "B" with his original scale. (and i wanted to change the originial "B" scale)

    My code:


    Vector3 newSimpleScale = new Vector3(60, 60, 60);
    
    // -----------------------------------------------------------------------------------------
      public void OnInstantiateRing()
      {
        photonView.RPC("InstantiateNewA", RpcTarget.AllBufferedViaServer);
      }
      [PunRPC]
      public void InstantiateNewA()
      {
        objectB = PhotonNetwork.Instantiate("ObjectA", new Vector3(0,0,0), Quaternion.Euler(0, 0, 0));
        objectB.name = $"NEW_{objectB.name}";
        objectB.transform.position = new Vector3(0.435f, 1.295f, 7.585f);
        objectB.transform.rotation = Quaternion.Euler(0, 0, 0);
      }
    
      // -----------------------------------------------------------------------------------------
      public void IncreaseInstantiateB() // scale new obj
      {
        photonView.RPC("IncreaseInstantiateB", RpcTarget.AllBufferedViaServer, newSimpleScale);
      }
      [PunRPC]
      public void IncreaseInstantiateB(Vector3 simpleScale)
      {
        objectB.transform.localScale = simpleScale;
      }
    
  • Tobias
    Options

    Define the new scale and material before you PN.Instantiate. Pass both values as instantiation data (the last, optional parameter of PN.Instantiate). This data is then available in IPunInstantiateMagicCallback which will run on all instances (local and remote).

    The docs should have some more info.

  • mesmes
    Options

    Thank you but unfortunately it did not work as I expected.

    The custom data is working (scale, rotation, position & parentName)

    In "OnPhotonInstantiate" function I first look for the parentName in the scene and then set it as parent for the new object, just after that I am using the custom data.

    The weird thing is that after instantiation the new obj I see him on the OtherPlayer hand and not on mine as expected.

  • Tobias
    Options

    If the parent has a PhotonView, you could refer to this view's viewID instead of using a name. Names are not synced, so .. I can imagine you find the wrong one.

  • mesmes
    Options

    Even after I found the parent by his view ID (Thank you for the great idea) when the object attached the just OTHERS players can see the obj and not the creator...

  • Tobias
    Options

    Can you describe the current state some more? I don't see how only the creator of an object should not see it...

  • mesmes
    mesmes
    edited June 2022
    Options

    I am building a VR multiplayer game with the oculus package.

    My goal is that all players in the room will be expose to an Object called "Gun 77".

    Prefab:

    Prefab "Gun 77" has the following components:

    • Sphere Collider
    • PhotonView [ → Ownership set to “TakeOver”]
    • Rigidbody [collision detection: Discrete, 2 checkboxes approve (use gravity, is kinematic)]
    • PhotonRigidbodyView [All 3 checkboxes approve (teleport, sync velocity, sync angular velocity)]
    • XR Grab Interactable

    Flow:

    When I first grab Gun 77 I want to:

    • Instantiate a the same prefab from resources (Gun 77)
    • Put him as a child of my player left hand (left hand has a photonView, PhotonAnimatorView)
    • Change his scale
    • I want the new gun I created will be exposed to everyone in the scene including me of course.

    Instantiation process:

    • I am using PhotonNetwork.Instantiation(...) and adding Custom Data to it such as (parent PhotonViewID and scale size)
    • I have a AssetCreator who is responsible for the creation of new guns with MonoBehaviourPunCallbacks and OnPhotonInstantiatd for getting the custom data.
    • AssetCreator don't have on him any photon view components attached.


    The Problem:

    1. The Client who trigger the event in AssetCreator for creating the prefab ( Gun 77 ) sees it at the correct size (after I changed it during instantiation), but others see it at the default size.
    2. Gun 77 (attached to the creator's left hand) moves smoothly, while others see it jumping between frames
    3. The problem occurs whether I am the master client or a regular client