Weird Exceptions Thrown on PhotonSerialize

Options
Hey folks!

I have been trying to find out what exactly triggers these exceptions:

IndexOutOfRangeException: Array index is out of range.
at PhotonStream.ReceiveNext () [0x0001e] in C:\Users\elenk\Downloads\Dominions at War\Assets\Photon Unity Networking\Plugins\PhotonNetwork\PhotonClasses.cs:1069
at WaypointMoving.IPunObservable.OnPhotonSerializeView (.PhotonStream stream, PhotonMessageInfo info) [0x00073] in C:\Users\elenk\Downloads\Dominions at War\Assets\Scripts\WaypointManagement\WaypointMoving.cs:453


The 453 line of my WaypointMoving script is:
void IPunObservable.OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info) {
		if (stream.isWriting) {
			if (agent.destination != targetWayPoint.transform.position) {
				
				stream.SendNext (agent.destination);
				targetWayPoint.transform.position = agent.destination;
			}
		} else {
			agent.destination = (Vector3)stream.ReceiveNext (); //this is the line throwing the Array index out of range exception
		}
	}
Also, I get another weird exception this time for casting:

InvalidCastException: Cannot cast from source type to destination type.
at BuildingSpawnUnits.OnPhotonSerializeView (.PhotonStream stream, PhotonMessageInfo info) [0x0008e] in C:\Users\elenk\Downloads\Dominions at War\Assets\Scripts\Building\BuildingSpawnUnits.cs:302


Where the line of code is:
public void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info) {

		if (stream.isWriting) {
			if (placedOnGround) {
				stream.SendNext (transform.position);
				stream.SendNext (transform.rotation);
			}

			stream.SendNext (placedOnGround);
			stream.SendNext (doneConstruction);
			stream.SendNext (currenthpBuilding);
			//stream.SendNext (spawningSeconds);

		} else {
			if (placedOnGround) {
				realPos = (Vector3)stream.ReceiveNext (); //this is the line of code throwing the cast exception
				realRot = (Quaternion)stream.ReceiveNext ();	
			}
			placedOnGround = (bool)stream.ReceiveNext ();
			doneConstruction = (bool)stream.ReceiveNext ();
			currenthpBuilding = (float)stream.ReceiveNext ();
			//spawningSeconds = (float)stream.ReceiveNext ();

		}
Any help would be ideal as I suspect that after a few minutes one of my clients crash due to these exceptions.

Thank you!

Comments

  • Hi @HeadlessFly,

    I currently don't know what happens if OnPhotonSerializeView is called on the 'sending' side and you don't write anything to the stream, what might happen in your case (inner condition). So firstly I would ask you, to remove the inner condition on the 'sending' side and see, if the error still exists afterwards. If so, we can take a closer look at the problem.

    To the second problem: I guess here it is basically a problem with the order of the data you send. Please try sending the 'placedOnGround' boolean as first value in order to have an up-to-date condition on the 'receiving' side.
  • Hey @Christian_Simon,

    I could solve my first issue with the inner condition in another way, so that is done.

    For the second problem - it actually solved my cast exception if I stream send the placedOnGround bool first and also receive it first.

    Thank you!