- Home /
Photon Network Text Problem
Hello im trying to update a "Counter" text for both players which change with a click for example click 99, click 98. How is it possible that the two players see the same text? If im clicking at the one player only there the number changes. Its my first time working with Photon so here is my Code:
public class Player : Photon.MonoBehaviour {
public Text CounterText;
private int number = 100;
// Use this for initialization
void Start () {
CounterText = GameObject.Find("Counter").GetComponent<Text>();
}
// Update is called once per frame
void Update() {
if (photonView.isMine)
{
if (Input.GetMouseButtonUp(0))
{
number -= 1;
CounterText.text = "Counter: " + number;
Debug.Log("Clicked");
}
}
}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(CounterText.text);
}
else
{
}
}
}
Thanks
Don't forget you need to receive as well.. I'm still new to photon too but try this..:
private void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(CounterText.text);
}
else
{
CounterText.text = (Text)stream.ReceiveNext();
}
}
Trying it like this now but still doesnt work . Text is still not changing
public void OnPhotonSerializeView(PhotonStream stream, Photon$$anonymous$$essageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(CounterText.text);
}
else
{
CounterText.text = (string)stream.ReceiveNext();
}
}
Answer by ChristianSimon · May 03, 2018 at 08:37 AM
Instead of using the OnPhotonSerializeView function, try using a RPC. Whenever a client clicks the left mouse button, you can send a RPC by using photonView.RPC("MethodName", PhotonTargets.All, number);
. You now have to implement the 'receiving' function as well. This function has to be marked with the [PunRPC] attribute, for example:
[PunRPC]
public void MethodName(int number, PhotonMessageInfo info) { }
Within this function you can update the text component with the number you have received.
For more information about RPCs I would recommend you taking a look at the RPCs and RaiseEvent documentation page.