[SyncVar] <user-defined struct> not syncing to client

I have an issue where my [SyncVar] float properties are being successfully synched from server to clients, but my [SyncVar] property is not.

According to the manual,

I’m fine with this–the values will only be updated once every few minutes, so efficiency is not a major concern.

My user-defined struct follows below. The struct contains float arrays, but also according to the manual,

Keep in mind that I am syncing only a single struct, not a list, but presumably the same rules apply to the contents of the single struct as to a member of list of structs.

public struct MyStruct
{
    public float[] Values1;
    public float[] Values2;
    public float[] Values3;
    public float[] Values4;
}

The implementation in my NetworkBehaviour can summarized as follows:

[SyncVar(hook="MyStructChanged")] MyStruct structValue;

// The event handler is only called on a server runtime, next to a library
// broadcasting value updates.
// Note:  this is the same pattern used for successfully setting
// [SyncVar] float values.  I suspected that I could try firing a [Command]
// from here, but since the float vars are working, it seems the issue is
// something different

void SomeEventHandler(object sender, BroadcastArgs e)
{
    structValue.Values1 = e.values1
}

// hook is never invoked
[Client]
void MyStructChanged(MyStruct newValue)
{
    structValue = newValue;
    UpdateUI();
}

The point which jumps out to me in the manual but which is not clearly documented is the note: “Note that setting a SyncVar member variable inside a property setter function does not cause it to be dirtied.” This seems similar to the issue described here. I suspect I need to explicitly invoke some structValue.SetDirty() function, but I’m not seeing an example of this approach. Am I on the right track?

1 Like

I believe the intended use is to recreate the struct each time (at least, this works for me). Pass in a new MyStruct instead of altering the variables of the existing struct.

1 Like

That did it. Thanks!

Alternatively you can manually SetDirtyBit. It would be nice if changing the struct automatically SetDirtyBit