Unity Crashes on Rpc call

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:

    public class ListSerializable<U>: INetworkSerializable where U : INetworkSerializable
    {
        public U[] Source { get; set; }
        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
            int length = 0;
            if (!serializer.IsReader)
            {
                length = Source.Length;
            }

            serializer.SerializeValue(ref length);

            if (serializer.IsReader && Source == null)
            {
                Source = new U[length];
            }
            for (int i = 0; i < length; i++)
            {
                Source[i].NetworkSerialize(serializer);
            }
        }
    }

Here unity crashes, on line 18:

        [ServerRpc(RequireOwnership = false)]
        private void GetAllProfilesServerRpc(ulong clientId)
        {
            if (!IsServer)
            {
                return;
            }
            var profiles = ServerGameManager.ConnectedPlayers;
            var serializableProfiles = new ListSerializable<ServerPlayerProfile>() { Source = profiles.ToArray() };
            Debug.Log($"Sending all profiles to client {clientId}");
            ClientRpcParams clientRpcParams = new ClientRpcParams
            {
                Send = new ClientRpcSendParams
                {
                    TargetClientIds = new ulong[] { clientId }
                }
            };
            GetAllProfilesClientRpc(serializableProfiles, clientRpcParams);
        }

When I replaced the generic part in the class with a specific one it’s stops crashing.

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.

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.

1 Like

Ok, thanks, good to know.