- Home /
Photon variable synchronization
Hello, I'm trying to synchronize a variable which changes whenever one of the players/clients pressed a button.
The problem is that it only works when the First player/Person who creates the room does it, if the second player who joins the created room clicks the buttons it changes the values for him and doesn't sync on the other player's pc.
Here's my code so far:
public float test;
void Update()
{
PhotonView photonView = this.photonView;
photonView.RPC("Test", PhotonTargets.All);
}
[PunRPC]
void Test()
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
test = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
test = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
test = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
test = 3;
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
test = 4;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(test);
}
else
{
test = (float)stream.ReceiveNext();
}
}
Answer by Ssiroo · Sep 13, 2016 at 09:20 PM
Fixed! If you use RPC calls then you no longer need to sync the variables in the OnPhotonSerialieView function.
Updated code:
[PunRPC]
void Test()
{
if (Input.GetKeyDown(KeyCode.Alpha0))
{
photonView.RPC("Lel", PhotonTargets.All, 0);
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
photonView.RPC("Lel", PhotonTargets.All, 1);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
photonView.RPC("Lel", PhotonTargets.All, 2);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
photonView.RPC("Lel", PhotonTargets.All, 3);
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
photonView.RPC("Lel", PhotonTargets.All, 4);
}
}
[PunRPC]
void Lel(int lewl)
{
test = lewl;
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
}
else
{
}
}
Your answer
Follow this Question
Related Questions
Unity multiplayer solutions: Photon, Unity Networking - what else and in what way is good? 0 Answers
Unity Photon Virtual Joystick Problem HELP???? 1 Answer
Unity3D: Photon syncing physics events 0 Answers
Multiplayer Game Disconnects 1 Answer
How can I create a simple chat system for my multiplayer game? 1 Answer