Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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. }
     
unityunity