- Home /
[PUN2] Object Active / Not Active
I wondered if I could get some assistance on PUN 2.
I have a script that means when the player presses "D" the object will change (One goes inactive, the other goes active)
public bool right;
public bool center;
public GameObject rightobj;
public GameObject centerobj;
Then on update I have this.
if (Input.GetKeyDown(KeyCode.D))
{
if (center == true)
{
if (photonView.IsMine)
{
centerobj.SetActive(false);
rightobj.SetActive(true);
right = true;
center = false;
}
else
{
centerobj.SetActive(false);
rightobj.SetActive(true);
}
}
I have also tried adding this to the bottom of the script.
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(rightobj.activeSelf);
stream.SendNext(centerobj.activeSelf);
}
else if (stream.IsReading)
{
rightobj.SetActive((bool)stream.ReceiveNext());
centerobj.SetActive((bool)stream.ReceiveNext());
}
}
}
I have literally no luck. When the player presses "D" the object will change locally but nothing will happen on the "Other Player" end.
Could someone advise a simpler solution if possible?
Thankyou very much
Answer by joshylad1997 · Dec 23, 2019 at 11:20 PM
You either need to do this via a RPC or through possibly through a custom player property. For this case an RPC is by the best option. If you are making a multiplayer game I would highly recommend reading and fully understanding RPC's as they are by far the most important thing when it comes to multiplayer games.
See photon documentation: https://doc.photonengine.com/en-us/pun/v2/gameplay/rpcsandraiseevent
If you still have any further questions let me know