Photon OnPhotonSerializeView not working

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;

public class Health : MonoBehaviour, IPunObservable
{

    public int health;


    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {

        if (stream.IsWriting)
        {
            stream.SendNext(health);

        }
        else
        {
            health = (int)stream.ReceiveNext();
        }
    }


    public void TakeDamage(int Damage)
    {
        health -= Damage;
    }
   
}

I assigned it in the photonview oberserved components
Thanks for help

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.

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

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?

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

Glad to read that.
Sometimes it’s good to use RPCs, sometimes you better use Custom Properties or synchronization via the PhotonView. We wrote a guide about state synchronization approaches for PUN 2.