InvalidCastException
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
InvalidCastException: Specified cast is not valid.Photon.Pun.PhotonTransformView.OnPhotonSerializeVi
SriharshaAntiz
2021-03-23 08:03:22
I'm getting this error on Pun 2.25
InvalidCastException: Specified cast is not valid.Photon.Pun.PhotonTransformView.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformView.cs:125)Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:734)Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:724)Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, System.Int32 networkTime, System.Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1821)Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2192)Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3249)
OnPhotonSerializeView() writes and reads the data as expected. And have checked the types as well.
Here is my OnPhotonSerializeView() function
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(PlayerObjectToSynch!=null)
{
if (stream.IsWriting)
{
stream.SendNext(PlayerObjectToSynch.gameObject.transform.position);
stream.SendNext(PlayerObjectToSynch.gameObject.transform.rotation);
stream.SendNext(PlayerObjectToSynch.currentPlayerAnimation);//currentTransportAnimation
stream.SendNext(PlayerObjectToSynch.currentTransportAnimation);
stream.SendNext(PlayerObjectToSynch.transportOnOff);
print("Am writing from" + PlayerObjectToSynch.name);
}
else if (stream.IsReading)
{
PlayerObjectToSynch.gameObject.transform.position = (Vector3)stream.ReceiveNext();
PlayerObjectToSynch.gameObject.transform.rotation = (Quaternion)stream.ReceiveNext();
currentAnimation = (string)stream.ReceiveNext();
transportCurrentAnimation = (string)stream.ReceiveNext();
tOnOf =(bool)stream.ReceiveNext();
print("Am reading to" + PlayerObjectToSynch.name);
}
}
}
Unable to figure out what exactly is causing the error.
Comments
Hi @SriharshaAntiz,
Thank you for choosing Photon and for your report!
Could you update to the latest PUN2 and retry?
When you try the PUN Basics Tutorial or Asteroids demos scenes that are provided in the package, can you reproduce?
Did you modify PhotonTransformView or any other PUN 2 code?
SriharshaAntiz
2021-03-23 13:08:04
Hi @JohnTube mod,
Thanks for looking in,
After updating the Photon these below errors are thrown.
1.
Assets/Photon/Simple/Components/SyncState/SyncOwner.cs(213,28): error CS1061: 'PhotonView' does not contain a definition for 'SetOwnerInternal' and no accessible extension method 'SetOwnerInternal' accepting a first argument of type 'PhotonView' could be found (are you missing a using directive or an assembly reference?)
2.
Assets/Photon/Simple/Core/NetMaster/NetMaster.cs(477,28): error CS1061: 'PhotonView' does not contain a definition for 'SetControllerInternal' and no accessible extension method 'SetControllerInternal' accepting a first argument of type 'PhotonView' could be found (are you missing a using directive or an assembly reference?)
I have tried to clear the errors, but I'm unable to, any help is much appreciated.
Thanks in advance :smile:
Hi @SriharshaAntiz,
Simple is no longer included in PUN2 package.
You need to download it and import it separately on top of PUN2.
Read more here.
You can also remove it from your project if you don't use it.
Or clean it up before updating it.
Folders: "Assets\Photon\PhotonUtilities" and "Assets\Photon\Simple."
Thank you for your understanding and sorry for the inconvenience!
SriharshaAntiz
2021-03-24 06:52:29
Hi @JohnTube mod,
I have followed your instructions and cleared all the errors and was able to run the project, but the above error persists even after the update.
Error after update,
InvalidCastException: Specified cast is not valid.
Photon.Pun.PhotonTransformView.OnPhotonSerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/Views/PhotonTransformView.cs:125)
Photon.Pun.PhotonView.DeserializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:616)
Photon.Pun.PhotonView.DeserializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs:606)
Photon.Pun.PhotonNetwork.OnSerializeRead (System.Object[] data, Photon.Realtime.Player sender, System.Int32 networkTime, System.Int16 correctPrefix) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:1864)
Photon.Pun.PhotonNetwork.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonUnityNetworking/Code/PhotonNetworkPart.cs:2235)
Photon.Realtime.LoadBalancingClient.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3289)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PeerBase.cs:891)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/EnetPeer.cs:558)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at D:/Dev/Work/photon-dotnet-sdk/PhotonDotNet/PhotonPeer.cs:1837)
Photon.Pun.PhotonHandler.Dispatch () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:223)
Photon.Pun.PhotonHandler.FixedUpdate () (at Assets/Photon/PhotonUnityNetworking/Code/PhotonHandler.cs:149)
To give an idea what i'm trying to achieve, I am trying to synchronise two players in my game and have done it using Photon's OnPhotonSerializeView() . The error occurs while running on unity editor, but bypasses it continues and both the players are shown (Player 1 runs on mac build and Player 2 runs on unity editor 2019.2.5f1)
But, the same is not reproduced on iOS devices. Able to view two players on only one device, the run device displays' its player alone.
I have tried it with simple function as below
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
Debug.Log("writer");
}
else if (stream.IsReading)
{
Debug.Log("reader");
}
}
Even without passing any stream writing and reading the error still occurs. Unable to figure out whether it is my coding mistake or a bug on Photon end.
Back to top