Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party PUN 2 - OnPhotonSerializeView not syncing a float value

Discussion in 'Multiplayer' started by Blipsoftdev, Feb 9, 2021.

  1. Blipsoftdev

    Blipsoftdev

    Joined:
    May 22, 2020
    Posts:
    5
    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 player script (shortened for the relevant code) - I can't figure out what I'm doing wrong, so some expert advise would be much appreciated.

    Code (csharp):
    1. public float playerCount;
    2.  
    3.  public void Start()
    4. {
    5.  playerCount = 0f;
    6. }
    7.  
    8.  
    9.  void Update()
    10.     {
    11.         if (PhotonNetwork.IsMasterClient)
    12.         {
    13.             if (playerCount >= GameManager.instance.playersToWin && !GameManager.instance.gameEnded)
    14.             {
    15.                 GameManager.instance.gameEnded = true;
    16.                 GameManager.instance.photonView.RPC("WinGame", RpcTarget.All);
    17.             }
    18.         }
    19.  
    20.  void OnTriggerEnter(Collider other)
    21. {
    22.  if (other.CompareTag("LevelEnd"))
    23.         {
    24.                 playerCount += 1;
    25. }
    26.  
    27. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    28.     {
    29.         if (stream.IsWriting)
    30.         {
    31.             stream.SendNext(playerCount);
    32.         }
    33.         else if (stream.IsReading)
    34.         {
    35.             playerCount = (float)stream.ReceiveNext();
    36.         }
    37.     }
    38.  
    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.
     
    Last edited: Feb 9, 2021
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    To sync values of a PhotonView script, you need to implement OnPhotonSerializeView and add the script into the "Observables" list of the PhotonView.
    Finally, use PhotonNetwork.Instantiate to get an instance of this object (with the scripts) in a room or load it with the scene.
    Most of this is shown in the PUN Basics Tutorial.
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    It also looks as if OnTriggerEnter is running on all instances of the networked object. So even if only one client will send updates via OnPhotonSerializeView, anyone may set playerCount.

    Why is it a float, by the way? You expect half a player? Could be an integer.
     
  4. Blipsoftdev

    Blipsoftdev

    Joined:
    May 22, 2020
    Posts:
    5
    Hi Tobiass. I changed the float to an int - (oops!) and included the following:

    Code (CSharp):
    1. if (other.CompareTag("LevelEnd"))
    2.         {
    3.             if (photonView.IsMine)
    4.              playerCount ++;
    5.  
    6.         }
    I had already added the public void OnPhotonSerializeView if that's what you mean? And the player controller script is in the "Observables" list of the PhotonView.

    Still no luck, I imagine it's something silly I've done but cannot work out what.
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    In OnPhotonSerializeView, you could add some debug log and make sure this is being sent / received.
    Or count the updates you get and put that on screen somewhere (whatever you like best).
    Are you instantiating a prefab, which has a PhotonView and this component?