- Home /
OnPhotonSerializeView InvalidCastException : Specified cast is not valid.
Onplayer :
void Update()
{
if (photonView.isMine)
{
mpos = Input.mousePosition;
}
}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(vur);
stream.SendNext(mpos);
}
else if (stream.isReading)
{
mpos = (Vector3)stream.ReceiveNext();
vur = (int)stream.ReceiveNext();
}
}
OnBall : private void OnCollisionStay(Collision collision) {
if (collision.gameObject.tag == "Player")
{
oyuncu = collision.gameObject;
script = oyuncu.GetComponent<oyuncukontrol>();
Ray ray = Camera.main.ScreenPointToRay(script.mpos);
RaycastHit hit;
if (script.vur == 0)
{
if (Physics.Raycast(ray, out hit, 100))
{
targetPos = hit.point;
}
}
}
}
}
Error :
InvalidCastException: Specified cast is not valid.
oyuncukontrol.OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info) (at Assets/Scripts/oyuncukontrol.cs:77)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)
PhotonView.ExecuteComponentOnSerialize (UnityEngine.Component component, PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:553)
PhotonView.DeserializeComponent (UnityEngine.Component component, PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:378)
PhotonView.DeserializeView (PhotonStream stream, PhotonMessageInfo info) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:363)
NetworkingPeer.OnSerializeRead (System.Object[] data, PhotonPlayer sender, System.Int32 networkTime, System.Int16 correctPrefix) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:4409)
NetworkingPeer.OnEvent (ExitGames.Client.Photon.EventData photonEvent) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2638)
ExitGames.Client.Photon.PeerBase.DeserializeMessageAndCallback (ExitGames.Client.Photon.StreamBuffer stream) (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.EnetPeer.DispatchIncomingCommands () (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
ExitGames.Client.Photon.PhotonPeer.DispatchIncomingCommands () (at <5b2c7915d6ca43908ac03c52eb97ef22>:0)
PhotonHandler.Update () (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:161)
Problem at mpos = (Vector3)stream.ReceiveNext();
Answer by Bunny83 · Mar 13, 2020 at 03:14 PM
When you serialize data into a stream you have to make sure that you keep the order right since the data is "serialized", i.e. arranged in a series. When you write / serialize your data you have those two lines:
stream.SendNext(vur);
stream.SendNext(mpos);
So you first serialize your "vur" value and then your "mpos" value. However when reading / deserializing you have this code:
mpos = (Vector3)stream.ReceiveNext();
vur = (int)stream.ReceiveNext();
So you first try to read your "mpos" value which of course doesn't work since the first value is your "vur" value. Since "vur" seems to be an integer of course the integer can not be casted / converted into a Vector3.
You have to swap the two lines, either when writing OR when reading so the order is the same.
if (stream.isWriting)
{
stream.SendNext(vur);
stream.SendNext(mpos);
}
else if (stream.isReading)
{
vur = (int)stream.ReceiveNext();
mpos = (Vector3)stream.ReceiveNext();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612192017im_/https://answers.unity.com/themes/thub/images/avi.jpg)