Syncing a class member's variables?

Hi I have a class called Stats which stores all units stats and I want to sync the player state.

So within my playerScript class which extends NetworkBehaviour, I have a stats class which is also a NetworkBehaviour which within it has a state variable that I want to sync.

Right now I’m getting an error that says:
InvokeCommand class [Stats] doesn’t match [PlayerScript])
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

I read somewhere last night this happens when you have two NetworkBehaviour scripts on an object but I can’t find the link where I read that.

Can I sync this or do I have to put the stuff I want to sync into one NetworkBehaviour?

 public class PlayerScript : NetworkBehaviour
{
    private Stats _stats;

    void UpdateInput()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            _stats.CmdSetState(Stats.State.Attacking);
        }
    }
}

public class Stats : NetworkBehaviour
{
    public enum State { Attacking, Moving, Idle }

    [SyncVar]
    public State CurrentState;

    [Command]
    public void CmdSetState(State state)
    {
        CurrentState = state;
    }
}

That’s a bug; it’s fixed in Patch 1 for Unity 5.1, which is out now.

1 Like

Tried updating and it says I’m up to date with version 5.1.0f3.

I think the problem is that the PlayerScript class is calling the Command on the Stats class and you shouldn’t do that?

I fixed it by making the PlayerScript extend the Stats class but still means you can’t have 2 NetworkBehaviours on a class?

public class PlayerScript : Stats
{
    void UpdateInput()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            CmdSetState(Stats.State.Attacking);
        }
    }
}

public class Stats : NetworkBehaviour
{
    public enum State { Attacking, Moving, Idle }

    [SyncVar]
    public State CurrentState;

    [Command]
    protected void CmdSetState(State state)
    {
        CurrentState = state;
    }
}

You should install 5.1p1 - it won’t show up in the update notifications in the editor (it’s a patch release). Get it here: Unity 5.1.0p1

1 Like