NetworkString script gives an error CS0315

out of nowhere the NetworkString script started giving an error saying
“CS0315: The type ‘Unity.Collections.FixedString32Bytes’ cannot be used as type parameter ‘T’ in the generic type or method ‘BufferSerializer.SerializeValue(ref T, FastBufferWriter.ForPrimitives)’. There is no boxing conversion from ‘Unity.Collections.FixedString32Bytes’ to ‘System.IComparable’.”

using Unity.Collections;
using Unity.Netcode;

public struct NetworkString : INetworkSerializable
{
    private FixedString32Bytes info;
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref info); // SerializeValue(ref info) is giving the error
    }

    public override string ToString()
    {
        return info.ToString();
    }

    public static implicit operator string(NetworkString s) => s.ToString();
    public static implicit operator NetworkString(string s) => new NetworkString() { info = new FixedString32Bytes(s) };
}
2 Likes

replace public struct NetworkString : INetworkSerializable with public struct NetworkString : INetworkSerializable, INetworkSerializeByMemcpy

3 Likes

Hi I have the same issue and replacing
public struct NetworkString : INetworkSerializable
with
public struct NetworkString : INetworkSerializable, INetworkSerializeByMemcpy

is not working,
how can I fix this?

using Unity.Collections;
using Unity.Netcode;

public struct NetworkString :  INetworkSerializeByMemcpy
{
    private ForceNetworkSerializeByMemcpy<FixedString32Bytes> _info;
    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref _info);
       
    }

    public override string ToString()
    {
        return _info.Value.ToString();
    }

    public static implicit operator string(NetworkString s) => s.ToString();
    public static implicit operator NetworkString(string s) => new NetworkString() { _info = new FixedString32Bytes(s) };
}

This should work.

2 Likes

@luke-unity can you please explain why we should use ForceNetworkSerializeByMemcpy? (It worked well with 1.0.0-pre.7)

It is because we made a breaking change which requires to use the ForceNetworkSerializeByMemcpy now. We are looking in bringing back the previous workflow in a future release.

1 Like

Thanks a lot for the answer.
It helped me fix an issue when upgrading to latest version of Netcode for gameobjects.
:slight_smile: