Search Unity

OnPhotonSerializeView not working, at all

Discussion in 'Connected Games' 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:
    187
    PlayerSync : MonoBehaviour
    PlayerSync : Photon.MonoBehaviour

    See the difference? :)
     
    biggsthecat likes 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:
      710
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,202
    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:
    2
    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
     
  7. Balawal-Sarao

    Balawal-Sarao

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

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,202
  9. vselockov

    vselockov

    Joined:
    May 10, 2018
    Posts:
    3
    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:
    2
    make sure you have

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

    BloodHound_PL

    Joined:
    May 12, 2016
    Posts:
    3
    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
    Fdudka likes this.
  12. ravi001

    ravi001

    Joined:
    Nov 18, 2017
    Posts:
    1
    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 ..
     
    jedures likes this.