diff --git a/Assets/Mirror/Runtime/MessagePacker.cs b/Assets/Mirror/Runtime/MessagePacker.cs index d871de630..13ff1eff8 100644 --- a/Assets/Mirror/Runtime/MessagePacker.cs +++ b/Assets/Mirror/Runtime/MessagePacker.cs @@ -39,7 +39,7 @@ public static void Pack(T message, NetworkWriter writer) where T : IMessageBa // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type - int msgType = GetId(typeof(T).IsValueType ? typeof(T) : message.GetType()); + int msgType = GetId(default(T) != null ? typeof(T) : message.GetType()); writer.WriteUInt16((ushort)msgType); // serialize message into writer @@ -126,7 +126,9 @@ internal static NetworkMessageDelegate MessageHandler(Action handler return; } - message = typeof(T).IsValueType ? default(T) : new T(); + // if it is a value type, just use defult(T) + // otherwise allocate a new instance + message = default(T) != null ? default(T) : new T(); message.Deserialize(reader); } catch (Exception exception) diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs index c0aebb8c3..152f47175 100644 --- a/Assets/Mirror/Runtime/NetworkConnection.cs +++ b/Assets/Mirror/Runtime/NetworkConnection.cs @@ -239,7 +239,7 @@ public bool InvokeHandler(T msg, int channelId) where T : IMessageBase // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type - int msgType = MessagePacker.GetId(typeof(T).IsValueType ? typeof(T) : msg.GetType()); + int msgType = MessagePacker.GetId(default(T) != null ? typeof(T) : msg.GetType()); MessagePacker.Pack(msg, writer); ArraySegment segment = writer.ToArraySegment();