Search Unity

Third Party Specified cast is not valid (OnPhotonSerializeView)

Discussion in 'Multiplayer' started by Nowamm, Mar 31, 2021.

  1. Nowamm

    Nowamm

    Joined:
    Aug 24, 2018
    Posts:
    18
    I'm trying to sync spriteRenderer.flipX but I'm getting a weird error saying "Specified cast is not valid". The sprite renderer is on the gameobject that the script is on.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class SpriteFlipObservable : MonoBehaviour, IPunObservable
    7. {
    8.     private SpriteRenderer spriteRenderer;
    9.  
    10.     void Start()
    11.     {
    12.         spriteRenderer = GetComponent<SpriteRenderer>();
    13.     }
    14.  
    15.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    16.     {
    17.         if (stream.IsWriting)
    18.         {
    19.             stream.SendNext(spriteRenderer.flipX);
    20.         }
    21.         else if (stream.IsReading)
    22.         {
    23.             spriteRenderer.flipX = (bool)stream.ReceiveNext();
    24.         }
    25.     }
    26. }
    27.  
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    You could try to cast flipX on the sending side explicitly:
    Code (CSharp):
    1. stream.SendNext((bool)spriteRenderer.flipX);
    You get the error when reading, correct?
    Which version of PUN is this?
     
  3. Nowamm

    Nowamm

    Joined:
    Aug 24, 2018
    Posts:
    18
    Adding a cast didn't change a thing. Also, I'm pretty sure the error is when reading and I'm using PUN version 2.29.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Attach the debugger and have a look at the content for the stream, when this error happens. You should be able to see each value which is sent.
    Maybe the content gives you some hint what was written in which order.

    The sent data per PhotonView does not change and the values also keep their order.
    Do you have other components which write or read?
     
  5. Nowamm

    Nowamm

    Joined:
    Aug 24, 2018
    Posts:
    18
    What do you mean by attach the debugger? Also there 3 more observables on the object.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
  7. Nowamm

    Nowamm

    Joined:
    Aug 24, 2018
    Posts:
    18
    I just realized that it wasn't even any observable on my player, it was something else but I don't know what.
    This is the error I'm getting:

    InvalidCastException: Specified cast is not valid.
    Photon.Pun.PhotonNetwork.AlmostEquals (System.Object one, System.Object two) (at C:/Users/User/Documents/Pixel Duel!/Pixel Duel/Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2051)
    Photon.Pun.PhotonNetwork.DeltaCompressionWrite (System.Collections.Generic.List`1[T] previousContent, System.Collections.Generic.List`1[T] currentContent) (at C:/Users/User/Documents/Pixel Duel!/Pixel Duel/Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1899)
    Photon.Pun.PhotonNetwork.OnSerializeWrite (Photon.Pun.PhotonView view) (at C:/Users/User/Documents/Pixel Duel!/Pixel Duel/Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1779)
    Photon.Pun.PhotonNetwork.RunViewUpdate () (at C:/Users/User/Documents/Pixel Duel!/Pixel Duel/Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1654)
    Photon.Pun.PhotonHandler.LateUpdate () (at C:/Users/User/Documents/Pixel Duel!/Pixel Duel/Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:173)
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    As long as I don't know what you send and or receive, I have a hard time helping.
    Please figure out which values cause this issue. If the issues are not what you sent, let me know what the original data was.
     
  9. Nowamm

    Nowamm

    Joined:
    Aug 24, 2018
    Posts:
    18
    The problem is that I'm not sending or receiving anything anymore. I removed all of the observable scripts that I had on my player and the problem persists.
     
  10. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    You can go to the line where the issue happens and either insert a debug break before it (to use the debugger to figure out happens) or insert code that writes whatever is happening to the logs.
     
  11. michealcaj

    michealcaj

    Joined:
    Aug 18, 2017
    Posts:
    191
    Can I write and read an entire class this way ?
     
  12. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    You can register a custom de/serialize method to customize which classes can be sent. The idea is to send the values that define the state of an instance, not the code or whatever is needed to keep the state.
    See Custom Types.
     
  13. ibx00

    ibx00

    Joined:
    Jul 17, 2018
    Posts:
    7
    I have this exact same issue. But it reprocuces with wierd step. The moment I select the object in the heirarchy with the inspector opened, this error pops up. Other than that, the data doesn't sync with the players joining afterwards.
    Here is my code:
    Code (CSharp):
    1.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    2.     {
    3.         if (stream.IsWriting)
    4.         {
    5.             //Debug.Log("writing");
    6.  
    7.             stream.SendNext(this.health);
    8.         }
    9.         else if (stream.IsReading)
    10.         {
    11.             //Debug.Log("reading");
    12.  
    13.             this.health = (float)stream.ReceiveNext();
    14.         }
    15.     }
    But all of a sudden, the issue just disappeared. Even the data has started syncing correctly.
    Anything I did differently was, I just toggled the "Observable Search" dropdown to "Manual" and back to "Auto Find All".
    I'm using version 2.39
     
  14. ibx00

    ibx00

    Joined:
    Jul 17, 2018
    Posts:
    7
    It happened again, in a different project this time.
    Retried the same step as the previous one, and it resolved.
    PhotonView -> Observable Search -> Change "Auto Find All" to "Manual" and then back to "Auto Find All". It may not resolve then and there, but after a few attempts, the issue resolves.