Send struct via command doesn't work

Greetings,
I’m trying to send a simple struct from client to host using a command but it doesn’t work. I have no problem sending strings or ints.

The whole code is very simple.

Here’s my struct:

struct Names
{
     int x {get;set;}
    string[] names{get;set;}

    public int GetX() {return x;}
    public string[] GetNames() {return names;}

//probably irrelevant concerning my problem but I'll put it anyways
    public void setNames(List<string> NamesToSet)
    {
        x = NamesToSet.Count();
        names = NamesToSet.ToArray();
    }
}

Then I call this method from a button :

public void SendNames()
{
Names names = new Names();
//Some code
CmdSendNames(names);
}

Which calls this method :

void CmdSendNames(Names names)
{
Debug.Log(names.GetX().ToString());
}

But nothing happens on the server side. Again, if I send directly a string it works fine. For instance, if instead of putting in parameters (Names names) I use (string[ ] names) it works (of course I also change the parameters sent when calling this function).

Also I have a warning : “The class / struct Names has no public or non-static fields to serialize”.
I don’t know what it means.

Any help would be very appreciated.

I think the struct needs to have serializable fields just like MonoBehaviours serializable fields to be sent. Currently ‘int x’ and ‘string[ ] names’ are not serialized… so the struct appears empty (which is why it’s throwing the warning).

Try making ‘int x’ serializable by either
1: Making it public or…
2: Adding [SerializeField] attribute to it

Also, the struct may need a [System.Serializable] attribute I’m not sure.

1 Like

Yes adding [SerializeField] did the trick ! Thank you !