Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Unity Crashes on Rpc call

Discussion in 'Netcode for GameObjects' started by spore9, Mar 22, 2022.

  1. spore9

    spore9

    Joined:
    Apr 19, 2020
    Posts:
    2
    Hi all, I was trying to create generic class for serializing a list of INetworkSerializable items, but unexpectedly unity crashes (version 2021.2.14f1) on the server-side when calling client RPC from the server.
    Class itself:
    Code (CSharp):
    1.     public class ListSerializable<U>: INetworkSerializable where U : INetworkSerializable
    2.     {
    3.         public U[] Source { get; set; }
    4.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    5.         {
    6.             int length = 0;
    7.             if (!serializer.IsReader)
    8.             {
    9.                 length = Source.Length;
    10.             }
    11.  
    12.             serializer.SerializeValue(ref length);
    13.  
    14.             if (serializer.IsReader && Source == null)
    15.             {
    16.                 Source = new U[length];
    17.             }
    18.             for (int i = 0; i < length; i++)
    19.             {
    20.                 Source[i].NetworkSerialize(serializer);
    21.             }
    22.         }
    23.     }
    Here unity crashes, on line 18:
    Code (CSharp):
    1.         [ServerRpc(RequireOwnership = false)]
    2.         private void GetAllProfilesServerRpc(ulong clientId)
    3.         {
    4.             if (!IsServer)
    5.             {
    6.                 return;
    7.             }
    8.             var profiles = ServerGameManager.ConnectedPlayers;
    9.             var serializableProfiles = new ListSerializable<ServerPlayerProfile>() { Source = profiles.ToArray() };
    10.             Debug.Log($"Sending all profiles to client {clientId}");
    11.             ClientRpcParams clientRpcParams = new ClientRpcParams
    12.             {
    13.                 Send = new ClientRpcSendParams
    14.                 {
    15.                     TargetClientIds = new ulong[] { clientId }
    16.                 }
    17.             };
    18.             GetAllProfilesClientRpc(serializableProfiles, clientRpcParams);
    19.         }
    When I replaced the generic part in the class with a specific one it's stops crashing.
     
    Last edited: Mar 22, 2022
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    653
    It may help to know the error and if it's on the client or server side, but I would hazard a guess the reader doesn't know how to deserialize generic types. Objects that share the same interface have the same problem and a separate value is needed to identify the object type. I've not tried generic types though so I could be wrong.
     
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Generic types are currently not supported in RPCs and crash the process currently when used. We're aware of this and are working on a fix.
     
    spore9 likes this.
  4. spore9

    spore9

    Joined:
    Apr 19, 2020
    Posts:
    2
    Ok, thanks, good to know.