Specified cast is not valid (OnPhotonSerializeView)

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class SpriteFlipObservable : MonoBehaviour, IPunObservable
{
    private SpriteRenderer spriteRenderer;

    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(spriteRenderer.flipX);
        }
        else if (stream.IsReading)
        {
            spriteRenderer.flipX = (bool)stream.ReceiveNext();
        }
    }
}

You could try to cast flipX on the sending side explicitly:

stream.SendNext((bool)spriteRenderer.flipX);

You get the error when reading, correct?
Which version of PUN is this?

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.

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?

What do you mean by attach the debugger? Also there 3 more observables on the object.

I mean using the VS debugger to look at what’s happening:

Or add Debug.Log() output where necessary to get some insight.

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)

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.

1 Like

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.

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.

Can I write and read an entire class this way ?

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.

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:

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            //Debug.Log("writing");

            stream.SendNext(this.health);
        }
        else if (stream.IsReading)
        {
            //Debug.Log("reading");

            this.health = (float)stream.ReceiveNext();
        }
    }

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

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.