Stream.IsReading portion of OnPhotonSerializeView not working.

Options
Hello!

Posted another post about OnPhotonSerializeView a few days ago and resolved that problem. Now I've run into another problem and I don't understand what I'm missing. I've taken my time to read through the pun documentation aswell as watching multiple tutorials but this still won't work for me.

Whenever I try to send a string value between clients using OnPhotonSerializeView stream.IsWriting seems to be responding through the if statement but stream.IsReading doesn't seem to respond resulting in nothing being received on the other client.

Thanks in advance!
using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI;

using Photon.Pun;
using Photon.Realtime;

public class Chat : UnityEngine.MonoBehaviour, IPunObservable
{
    #region public 

    public InputField chatField;
    public GameObject chatUI;

    #endregion

    #region private

    string myText;
    bool sendMessage;

    #endregion

    #region private methods

    void Update()
    {
        InputListener();
    }

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

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

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        print("OnPhotonSerializeView");
        if (stream.IsWriting)
        {
            if (sendMessage) { stream.SendNext(myText); sendMessage = false; print("Sending Message"); }
            stream.SendNext(myText);
        }
        else if (stream.IsReading)
        {
            print("Stream is reading");
            string incomingText = (string)stream.ReceiveNext(); print("Incoming Text: " + 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
}

Comments

  • Tobias
    Options
    Which version of PUN is this?
    Did you use PhotonNetwork.Instantiate() to create the prefab with the PhotonView on it?

    Build a client and let it enter a room first. Then use the Editor to join and check the object's PhotonView state (you can use the inspector and debug mode). Check if the object is having the same viewID and if it is controlled locally.
  • It's Pun 2, If that's what you mean.

    The object is already in the scene and is not being instantiated.

    Usually I create the room with the editor and join it with the build. However this time I tested what you said and did the opposite and I could see the messages from the build to the editor console.

    From what I understand all clients must have their own instance of this script meaning that every client is the owner of a photonview component observing this script in order to be able to both write and read. Could you elaborate and explain if I'm right or not and also how this works?

    Thanks for the response!
  • mody
    mody
    edited June 2021
    Options
    If you are setting PhotonView.ViewID from the script, remove it. this worked for me.
  • Tobias
    Options
    I mean, which version is this specifically?
    It is not possible to write and read for one object on multiple clients. Which information will you consider correct then?
    How about instantiating an object per player?
    If each user just needs to send something, use RaiseEvent...