Search Unity

Third Party Photon OnPhotonSerializeView not working

Discussion in 'Multiplayer' started by Ewu_Uwe, Jan 5, 2021.

  1. Ewu_Uwe

    Ewu_Uwe

    Joined:
    Apr 13, 2020
    Posts:
    21
    I make a multiplayer game with photon and wanted to sync the health of the players.But it doesn't work, this is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Photon.Pun;
    6.  
    7. public class Health : MonoBehaviour, IPunObservable
    8. {
    9.  
    10.     public int health;
    11.  
    12.  
    13.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    14.     {
    15.  
    16.         if (stream.IsWriting)
    17.         {
    18.             stream.SendNext(health);
    19.  
    20.         }
    21.         else
    22.         {
    23.             health = (int)stream.ReceiveNext();
    24.         }
    25.     }
    26.  
    27.  
    28.     public void TakeDamage(int Damage)
    29.     {
    30.         health -= Damage;
    31.     }
    32.    
    33. }
    34.  
    I assigned it in the photonview oberserved components
    Thanks for help
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067
    The code looks ok. If it's causing issues, the next part would be to check if this actually runs.
    Add a Debug.Log("OnPhotonSerializeView") as first line or maybe inside the IsWriting case. Check the logs.
    Two players have to be in the room, before PUN calls any OnPhotonSerializeView.
    If this is not called, make sure it's in the Observed Components list of the PhotonView, on a prefab, which you then instantiate as networked object.
     
  3. Ewu_Uwe

    Ewu_Uwe

    Joined:
    Apr 13, 2020
    Posts:
    21
    OnPhotonSerializeView gets called, isWriting is true, and when the other player is moving, for example, its false.
    As is wrote, I assigned it's in the Observed Components list. And its instantiated with PhotonNetwork.Instantiate
     
    Last edited: Jan 6, 2021
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067
    > and when the other player is moving, for example, its false.
    The question would be: Is it called for the remote player with the incoming data? OnPhotonSerializeView gets called on the receiving side as well with isWriting = false. Then you have to use the data coming in.
    Or is anything in the logs that says it could not be sent?
     
  5. Ewu_Uwe

    Ewu_Uwe

    Joined:
    Apr 13, 2020
    Posts:
    21
    Thanks for your help, i figured out why it didn't work, the other player changed the value,but he is just receiving it. I madeit with an rpc and it works
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,067