SetActive(false) object for all players in room

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

SetActive(false) object for all players in room

mesmes
2022-04-28 07:43:56

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 ?

Comments

Tobias
2022-04-28 13:00:10

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
2022-04-28 14:48:43

But after creating "B" from "A" using thePhotonNetwork.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
2022-04-29 13:38:23

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
2022-05-02 14:32:14

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

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

In "OnPhotonInstantiate" function I firstlook 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
2022-05-03 14:43:11

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
2022-06-15 07:01:20

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
2022-06-15 11:28:29

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

mesmes
2022-06-17 11:07:45

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 77I 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 AssetCreatorwho is responsible for the creation of new guns with MonoBehaviourPunCallbacksand OnPhotonInstantiatd

  • for getting the custom data.

AssetCreator

  • don't have on him any photon view components attached.

Instantiation.png

Uploaded 2022-06-17T11:23:24+00:00 130284 bytes The Problem:

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

  • Gun 77 (attached to the creator's left hand) moves smoothly, while others see it jumping between frames

  • The problem occurs whether I am the master client or a regular client

Back to top