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();
}
}
}
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?
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.
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.
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.