Search Unity

  1. We have added dedicated sub-forums for UNet, the experimental MLAPI and Unity Transport Package. Please use the new forums to discuss related topics.
    Dismiss Notice
  2. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

[PhotonPUN] Serialize array of custom types

Discussion in 'Multiplayer' started by kqmdjc8, Apr 15, 2021.

  1. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    I have successfully managed to serialize my class using Photon docs and sending one BuildingData via RPC works fine. However my goal is to send an array of those objects.

    When trying to send an array, I'm getting this error:
    NullReferenceException: Object reference not set to an instance of an object

    In this line of code (full code will be below)
    Protocol.Serialize(buildingData.x, bytes, ref index);


    Full code:

    Code (CSharp):
    1. public class BuildingData
    2. {
    3.     public int x;
    4.     public int y;
    5.     public int type;
    6. }
    Code (CSharp):
    1. public class CustomPhotonSerialization
    2. {
    3.     public static readonly byte[] buildingDataMemoryBuffer = new byte[12];
    4.  
    5.     public static short SerializeBuildingData(StreamBuffer outStream, object customobject)
    6.     {
    7.         BuildingData buildingData = ((BuildingData) customobject);
    8.         lock (buildingDataMemoryBuffer)
    9.         {
    10.             byte[] bytes = buildingDataMemoryBuffer;
    11.             int index = 0;
    12.             Protocol.Serialize(buildingData.x, bytes, ref index);
    13.             Protocol.Serialize(buildingData.y, bytes, ref index);
    14.             Protocol.Serialize(buildingData.type, bytes, ref index);
    15.             outStream.Write(bytes, 0, 12);
    16.         }
    17.  
    18.         return 12;
    19.     }
    20.  
    21.     public static object DeserializeBuildingData(StreamBuffer inStream, short length)
    22.     {
    23.         BuildingData buildingData = new BuildingData();
    24.  
    25.         lock (buildingDataMemoryBuffer)
    26.         {
    27.             inStream.Read(buildingDataMemoryBuffer, 0, 12);
    28.             int index = 0;
    29.             Protocol.Deserialize(out buildingData.x, buildingDataMemoryBuffer, ref index);
    30.             Protocol.Deserialize(out buildingData.y, buildingDataMemoryBuffer, ref index);
    31.             Protocol.Deserialize(out buildingData.type, buildingDataMemoryBuffer, ref index);
    32.         }
    33.  
    34.         return buildingData;
    35.     }
    36. }
    Code (CSharp):
    1. public class SerializationTest : MonoBehaviour
    2. {
    3.     private PhotonView photonView;
    4.  
    5.     private void Start()
    6.     {
    7.         photonView = GetComponent<PhotonView>();
    8.  
    9.         PhotonPeer.RegisterType(typeof(BuildingData), 255, CustomPhotonSerialization.SerializeBuildingData, CustomPhotonSerialization.DeserializeBuildingData);
    10.     }
    11.  
    12.     [Button]
    13.     public void SendData()
    14.     {
    15.         BuildingData buildingData1 = new BuildingData();
    16.         buildingData1.x = 2;
    17.         buildingData1.y = 1;
    18.         buildingData1.type = 37;
    19.      
    20.         BuildingData buildingData2 = new BuildingData();
    21.         buildingData2.x = 3;
    22.         buildingData2.y = 5;
    23.         buildingData2.type = 28;
    24.      
    25.         BuildingData[] datas = new BuildingData[2];
    26.      
    27.         photonView.RPC("TestSingle", RpcTarget.AllViaServer, buildingData1);
    28.         photonView.RPC("TestArray", RpcTarget.AllViaServer, datas);
    29.     }
    30.  
    31.     [PunRPC]
    32.     public void TestSingle(BuildingData buildingData)
    33.     {
    34.         Debug.Log($"Received: {buildingData.x}, {buildingData.y}, {buildingData.type}");
    35.     }
    36.  
    37.     [PunRPC]
    38.     public void TestArray(BuildingData[] buildingDatas)
    39.     {
    40.         foreach (BuildingData buildingData in buildingDatas)
    41.         {
    42.             Debug.Log($"Received: {buildingData.x}, {buildingData.y}, {buildingData.type}");
    43.         }
    44.     }
    45. }
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,638
    And which object is Null?
     
  3. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    Line #12 from CustomPhotonSerialization so it seems that buildingData is null.
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,638
    That should only happen if you send a null BuildingData. In your code, datas is an array of null BuildingData.
     
unityunity