Search Unity

Third Party OnPhotonSerializeView not working, at all

Discussion in 'Multiplayer' started by EncounterStudios, May 1, 2015.

  1. EncounterStudios

    EncounterStudios

    Joined:
    May 1, 2015
    Posts:
    7
    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... :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(PhotonView))]
    5. public class PlayerSync : MonoBehaviour {
    6.  
    7.     [HideInInspector] public Vector3 position;
    8.     [HideInInspector] public Quaternion rotation;
    9.  
    10.     void Update() {
    11.         Debug.Log ("Position: " + position + " | Rotation: " + rotation);
    12.     }
    13.  
    14.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    15.         if (stream.isWriting) {
    16.             Debug.Log ("Is Writing");
    17.             stream.SendNext (transform.position);
    18.             stream.SendNext (transform.rotation);
    19.         } else {
    20.             Debug.Log ("Is Reading");
    21.             position = (Vector3)stream.ReceiveNext ();
    22.             rotation = (Quaternion)stream.ReceiveNext ();
    23.         }
    24.     }
    25. }
     
  2. EncounterStudios

    EncounterStudios

    Joined:
    May 1, 2015
    Posts:
    7
    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...
     
  3. Sehlor

    Sehlor

    Joined:
    Feb 10, 2012
    Posts:
    199
    PlayerSync : MonoBehaviour
    PlayerSync : Photon.MonoBehaviour

    See the difference? :)
     
    DotProductSoft and biggsthecat like this.
  4. metarvrs

    metarvrs

    Joined:
    May 23, 2017
    Posts:
    1
    @Sehlor

    It still doesn't work.
    Script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [RequireComponent(typeof(PhotonView))]
    7. public class PhotonTest : Photon.MonoBehaviour {
    8.  
    9.     int x;
    10.  
    11.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    12.     {
    13.         Debug.Log ("Serailizing");
    14.         if (stream.isWriting) {
    15.             int y = 1;
    16.  
    17.             Debug.Log ("writing to server");
    18.         } else {
    19.             Debug.Log ("reading from server");
    20.         }
    21.     }
    22. }
    23.  
    and I spawn the player using: PhotonNetwork.Instantiate ("serial", spawnPoint [playerCount].position, Quaternion.identity, 0);
     

    Attached Files:

    • ca.PNG
      ca.PNG
      File size:
      27.9 KB
      Views:
      1,459
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    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.
     
  6. Pawel_Legowski

    Pawel_Legowski

    Joined:
    Oct 28, 2015
    Posts:
    4
    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 :D
     
    Ahmet_Dem likes this.
  7. Balawal-Sarao

    Balawal-Sarao

    Joined:
    Jun 21, 2017
    Posts:
    3
    Same problem !!
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
  9. vselockov

    vselockov

    Joined:
    May 10, 2018
    Posts:
    10
    What the heck is wrong with people? no one answer, all use unity net(not)working which is laggy and just bad
     
  10. Carocrazy132

    Carocrazy132

    Joined:
    Aug 16, 2015
    Posts:
    15
    make sure you have

    Code (CSharp):
    1. using Photon;
    2. using Photon.Pun;
     
  11. BloodHound_PL

    BloodHound_PL

    Joined:
    May 12, 2016
    Posts:
    4
    It works for me. Make sure you have these lines in your script:

    Code (CSharp):
    1. using Photon.Pun;
    Code (CSharp):
    1. public class MyClass : MonoBehaviour, IPunObservable //Don't forget to implement the interface.
    In your Start() or any initializing method that you use:
    Code (CSharp):
    1. PhotonView pv = GetComponent<PhotonView>();
    2.  
    3. //after starting your game in editor you should see this component on the list of
    4. //Observed Components in your Inspector after selecting synchronized object.
    5. if (pv) pv.ObservedComponents.Add(this);
    6.  
    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).
     
    Last edited: Jun 6, 2019
  12. Gamer98898

    Gamer98898

    Joined:
    Nov 18, 2017
    Posts:
    12
    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 ..
     
    iasarmientoj, DrDigg0R and jedures like this.
  13. ishowta

    ishowta

    Joined:
    Dec 7, 2018
    Posts:
    1
    OnPhotonSerializeView can only be updated by the owner. It seems to use RPC or Custom Property of Room
     
  14. Altaireon

    Altaireon

    Joined:
    Oct 23, 2018
    Posts:
    7
    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.
     
  15. docktornicki

    docktornicki

    Joined:
    Oct 11, 2018
    Posts:
    3
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Realtime;
    5. using Photon.Pun;
    6.  
    7. public class ScoreStream : Photon.MonoBehaviour, IPunObservable
    8. {
    9.     public GameManager gm;
    10.     public new PhotonView photonView;
    11.     public int[] playerScore;
    12.     public bool isActive;
    13.     public const byte PlayerScoresEventCode = 1;
    14.  
    15.     void Start()
    16.     {
    17.         PhotonView pv = GetComponent<PhotonView>();
    18.         gm = GameObject.Find("GameManager").GetComponent<GameManager>();
    19.         isActive = false;
    20.  
    21.         PhotonNetwork.sendRate = 20;
    22.         PhotonNetwork.sendRateOnSerialize = 5;
    23.     }
    24.  
    25.  
    26.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    27.     {
    28.         if (stream.isWriting)
    29.         {
    30.             for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
    31.             {
    32.                 if (PhotonNetwork.playerList[i].name == PhotonNetwork.playerName)
    33.                 {
    34.                     stream.SendNext(gm.score);
    35.                 }
    36.             }
    37.         }
    38.         else
    39.         {
    40.             for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
    41.             {
    42.                 if (PhotonNetwork.playerList[i].name == PhotonNetwork.playerName)
    43.                 {
    44.                     playerScore[i] = (int)stream.ReceiveNext();
    45.                 }
    46.             }
    47.         }
    48.  
    49.         isActive = true;
    50.     }
    51. }
     
  16. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
  17. leontogamer

    leontogamer

    Joined:
    Feb 18, 2021
    Posts:
    1
    You need to add a IPunObservable in order for OnPhotonSerializeView to work
     
  18. Vivraan

    Vivraan

    Joined:
    Feb 2, 2018
    Posts:
    26
    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.
     
  19. unity_V1WlANEebEQloQ

    unity_V1WlANEebEQloQ

    Joined:
    Jul 1, 2019
    Posts:
    9
    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??
     

    Attached Files: