Search Unity

Question Serialize Struct Array

Discussion in 'Netcode for GameObjects' started by herrmutig, Nov 10, 2021.

  1. herrmutig

    herrmutig

    Joined:
    Nov 30, 2020
    Posts:
    22
    Hi,

    I have the following struct.

    Code (CSharp):
    1.     public struct TestStruct: INetworkSerializable
    2.     {
    3.         public int a;
    4.         public bool b;
    5.  
    6.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    7.         {
    8.             serializer.SerializeValue(ref a);
    9.             serializer.SerializeValue(ref b);        
    10.         }
    11.     }
    The following works:
    Code (CSharp):
    1. [ServerRpc]
    2. void MyServerRpc(TestStruct t)
    3. {
    4.    // do nothing for debugging purpose.
    5. }
    But when I change TestStruct to an array and pass it (the array is initialized!!), I get an Error.

    NullReferenceException

    This does not work. But why?
    Code (CSharp):
    1. [ServerRpc]
    2. void MyServerRpc(TestStruct[] t)
    3. {
    4.    // do nothing for debugging purpose.
    5. }