Issues with OnPhotonSerializeView

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Issues with OnPhotonSerializeView

Demonitized
2021-01-14 21:38:21

Hello!

I'll cut straight to the chase. I'm having a problem with OnPhotonSerializeView as it as I understand it, should be called a few times per second. However it doesn't seem to get called at all. I've got the IPunObservable as script type aswell as MonobehaviourPunCallbacks and it just won't work. I don't know what to do and I need this to work to be able to continue with my work.

Thanks in advance!

Comments

Jimbo
2021-01-16 22:55:56

Does the GameObject you're syncing have a PhotonView, and is it identifying your script as a component to sync? Showing how you implemented OnPhotonSerializeView might help too

Demonitized
2021-01-17 21:31:09

Hello!

Should there be a photonview component on the same object as the script then? If that's the case that could well be the problem but I'll paste my entire script here aswell.

Thanks for the response!


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

using UnityEngine;  
using UnityEngine.UI;

using Photon.Pun;  
using Photon.Realtime; 

public class Chat : MonoBehaviourPunCallbacks, IPunObservable  
{  
    #region public 

    public InputField chatField;  
    public GameObject chatUI;

    #endregion

    #region private

    string myText;  
    

    #endregion

    #region private methods

    void Update()  
    {  
        InputListener();  
    }

    private void InputListener()  
    {  
        if (Input.GetKeyDown(KeyCode.Return) && myText.Length > 0)  
        {  
              
            chatField.text = "";  
        }

        if (Input.GetKeyDown(KeyCode.Escape))  
        {  
            CloseChat();  
        }  
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)  
    {  
        print("OnPhotonSerializeView");  
        if (stream.IsWriting)  
        {  
            stream.SendNext(PhotonNetwork.NickName);  
        }     
        else if (stream.IsReading) { string incomingText = (string)stream.ReceiveNext(); print(incomingText); }  
    }

    #endregion

    #region public methods

    public void UpdateMessage() //Updates string as text is typed into the chatfield  
    {  
        myText = chatField.text;   
    }

    public void OpenChat()  
    {  
        chatUI.SetActive(true);  
    }

    public void CloseChat()  
    {  
        chatUI.SetActive(false);  
    }   

    #endregion  
}  

Demonitized
2021-01-18 12:05:41

Hello again!

Found the problem.

Apparently OnPhotonSerializeView isn't called if there aren't two players in a room and as I only tested with one player it didn't get called. Also seems I forgot a photonView component as you mentioned.

Thanks for everything!

Back to top