OnPhotonSerializeView - Help needed

Hey guys. So I'm fairly new to Unity, having been using it for about a year. I decided to throw myself in to the world of PUN2 and develop a simple co-op physics platformer as a portfolio piece.

I'm having an issue however when trying to sync a float value across the network. I have two instantiated players with Photon views, and I want the player to set a float value when they are both at the level end goal.

This is my code (shortened) - I can't figure out what I'm doing wrong, so some expert advise would be much appreciated.
public float playerCount;

 public void Start()
{
 playerCount = 0f;
}


 void Update()
    {
        if (PhotonNetwork.IsMasterClient)
        {
            if (playerCount >= GameManager.instance.playersToWin && !GameManager.instance.gameEnded)
            {
                GameManager.instance.gameEnded = true;
                GameManager.instance.photonView.RPC("WinGame", RpcTarget.All);
            }
        }

 void OnTriggerEnter(Collider other)
{
 if (other.CompareTag("LevelEnd"))
        {
                playerCount += 1;
}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(playerCount);
        }
        else if (stream.IsReading)
        {
            playerCount = (float)stream.ReceiveNext();
        }
    }

playersToWin float in GameManager is set to 2, with the idea once both players are colliding with the level end collider it'll call the function 'WinGame' as the playerCount will be 2 when synced.

I can see the float value changing in the inspector when the player collides, but only on the local player and not the clone - despite using OnPhotonSerializeView.

Sorry if I sound clueless. I might have jumped in too deep with PUN2, but I'm enjoying every minute of learning from mistakes - this one is giving me a headache though.



Comments

  • This looks a lot like some code I already gave some comment for.
    Did you post this multiple times?
    Did you find a solution?