OnPhotonSerializeView not working, at all

The OnPhotonSerializeView function doesn’t get called, at all… I have the script observed by the Photon View component but the stream doesn’t read nor write anything at all. I’ve never ran into this issue before, it have been working fine in my other projects but now it’s not.

The Update() function gets called and it writes to the console… But nothing in the console from the OnPhotonSerializeView function.

The code below is just a example, but still, it doesn’t work…

(I am connected to the internet, my network player instantiates as it should and so forth…) Everything else works fine, except this… :slight_smile:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PhotonView))]
public class PlayerSync : MonoBehaviour {

    [HideInInspector] public Vector3 position;
    [HideInInspector] public Quaternion rotation;

    void Update() {
        Debug.Log ("Position: " + position + " | Rotation: " + rotation);
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
        if (stream.isWriting) {
            Debug.Log ("Is Writing");
            stream.SendNext (transform.position);
            stream.SendNext (transform.rotation);
        } else {
            Debug.Log ("Is Reading");
            position = (Vector3)stream.ReceiveNext ();
            rotation = (Quaternion)stream.ReceiveNext ();
        }
    }
}

Oh, forgot to mention… The stream doesn’t even write or read when there are a minimum of two players… If there’s only one, the stream doesn’t write or read by default so I have tried to run two instances of my game and still it doesn’t work…

PlayerSync : MonoBehaviour
PlayerSync : Photon.MonoBehaviour

See the difference? :slight_smile:

2 Likes

@Sehlor

It still doesn’t work.
Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(PhotonView))]
public class PhotonTest : Photon.MonoBehaviour {

    int x;

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        Debug.Log ("Serailizing");
        if (stream.isWriting) {
            int y = 1;

            Debug.Log ("writing to server");
        } else {
            Debug.Log ("reading from server");
        }
    }
}

and I spawn the player using: PhotonNetwork.Instantiate (“serial”, spawnPoint [playerCount].position, Quaternion.identity, 0);

That totally looks like it should work…
Nothing in the console logs?
Maybe if you re-do the prefab? We had inexplicable cases where (much earlier), we just recreated the prefab and things worked out?

Oh. And make sure the “observed” component is a drag and drop of the prefab (not the instance which created the prefab). This could be something to look out for.

I suppose that You have not added your script into Observed Components list in Photon View of your game object. Had the same issue before I realised how stupid was that mistake :smile:

1 Like

Same problem !!

@Balawal-Sarao : Did the suggestions help? Could you fix? How?

What the heck is wrong with people? no one answer, all use unity net(not)working which is laggy and just bad

make sure you have

using Photon;
using Photon.Pun;

It works for me. Make sure you have these lines in your script:

using Photon.Pun;
public class MyClass : MonoBehaviour, IPunObservable //Don't forget to implement the interface.

In your Start() or any initializing method that you use:

PhotonView pv = GetComponent<PhotonView>();

//after starting your game in editor you should see this component on the list of
//Observed Components in your Inspector after selecting synchronized object.
if (pv) pv.ObservedComponents.Add(this);

Code whatever you need in OnPhotonSerializeView(), I recommend putting some Debug.Log(“Hello!”) right in the beginning just for initial testing. You won’t get any results, if you are alone in your room. In my case masterClient was writing something very often but the other client got stuff only on change (Unreliable On Change option of your PhotonView Component).

4 Likes

Hello Everyone… Please help … I am not able to send value from client to Master Client using OnPhotonSerializeView()
But its working in like only in one way while sending value from Master Client to Client …any help would be appericiable …

3 Likes

OnPhotonSerializeView can only be updated by the owner. It seems to use RPC or Custom Property of Room

In my case it would normally get hit when I have at least 2 client connected to the same room. If I’m alone in the room it wont get hit.

I am trying to do the same thing but i just cant get it to work, i have it in observed components and did all the things you said but still it doesnt work. Here is my Code. I am trying to send the player scores to all devices so i can make a scoreboard at the end of the game.

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

public class ScoreStream : Photon.MonoBehaviour, IPunObservable
{
    public GameManager gm;
    public new PhotonView photonView;
    public int[] playerScore;
    public bool isActive;
    public const byte PlayerScoresEventCode = 1;

    void Start()
    {
        PhotonView pv = GetComponent<PhotonView>();
        gm = GameObject.Find("GameManager").GetComponent<GameManager>();
        isActive = false;

        PhotonNetwork.sendRate = 20;
        PhotonNetwork.sendRateOnSerialize = 5;
    }


    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
            {
                if (PhotonNetwork.playerList[i].name == PhotonNetwork.playerName)
                {
                    stream.SendNext(gm.score);
                }
            }
        }
        else
        {
            for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
            {
                if (PhotonNetwork.playerList[i].name == PhotonNetwork.playerName)
                {
                    playerScore[i] = (int)stream.ReceiveNext();
                }
            }
        }

        isActive = true;
    }
}

As the clients are authoriative, each user could set her own score as a Player Property. Player.SetCustomProperty is used then.

You need to add a IPunObservable in order for OnPhotonSerializeView to work

I had a similar issue where some simple primitive fields were not getting synced between clients.

I’d done all the ceremony with the interface implementation: IPunObservable is included in the class declaration, and I’d tried both public and explicit implementations to no avail. Since I’m using a newer version of PUN 2, the Observables are auto included in the PhotonView component in the Editor.

Could it be a prefab serialisation issue?

EDIT: fixed the problem by refactoring some network unrelated code.

I am having a problemwith onPhotonSerialize view.It is not getting the updated of bool from update method.I have set bool to true if it collide with hurdle, and it changes to true inside other functons but not over here.Whats the problem with my code.Any Suggestions??