Need Help: How to Sync variables inside of a class

I’m trying to make a multiplayer game but I’ve run into a problem, i’m trying to sync the values inside of a class

[System.Serializable]
public class BaseStat
{
    public string name;
    public float value;
    public float max = 100;
    public float min = 0;
}

But when the client takes damage it doesn’t update on their end, but it updates on the server

//Used to change a BaseStat by calling the string(TODO: change to number id) and comparing it

    public List<BaseStat> stats = new List<BaseStat>();
    //Wont sync
    [SyncVar]
    public BaseStat Health;
    //Wont sync
    [SyncVar]
    public BaseStat Speed;

    //Test var to see if it would sync
    [SyncVar]
    public float syncHealth;
    [SyncVar]
    public float syncSpeed;

    //Is called when a projectile hits, used to send damage to a entity
    public void ApplyDamage(int damage)
    {
        CmdChangeStat("Health", -damage);
    }

    [Command]
    public void CmdChangeStat(string afflicted, float statChange)
    {
        BaseStat stat = FindStat(afflicted);
        float newStatvalue = stat.value + statChange;

        if (newStatvalue > stat.max)
        {
            newStatvalue = stat.max;
        }
        else if(newStatvalue < stat.min)
        {
            stat.value = stat.min;
        }
        else
        {
            stat.value += statChange;
        }

        syncHealth = Health.value;
        syncSpeed = Speed.value;
        RpcSyncStats();
        CmdSyncStats();
    }
    //Dosn't work; ment to change health of client
    [ClientRpc]
    void RpcSyncStats()
    {
        Health.value = syncHealth;
        Speed.value = syncSpeed;
    }
    //Check if server is also synced up
    [Command]
    void CmdSyncStats()
    {
        Health.value = syncHealth;
        Speed.value = syncSpeed;
    }

If anyone has any ideas on how to solve this I would greatly appreciate the help

I don’t have any answers to your actual question, but please look at this page for how to insert code more nicely into the forums (you can edit your current post + keep it in mind for future ones):Using code tags properly - Unity Engine - Unity Discussions

Thanks for the advice now its much easier to read

Sorry, I’m of next to no help here too because I haven’t done multiplayer yet, but I partly went through one of the tutorials on it. If you haven’t done it yet, I’d really suggest doing the same because it covers topics like that. I just can’t remember what all was involved. Unfortunately it looks like the Unity homepage is down for maintenance so this link won’t work currently, but I believe this was the tutorial:

Pass the new values in your RPC, otherwise clients don’t know what they are. Also no reason to make an RPC and Command that do the same thing, just use the RPC and it will send it to all clients (including the host)

2 Likes

That worked thanks, in case someone has a similar issue in future the code changes were

//Used to change a BaseStat by calling the string(TODO: change to number id) and comparing it
    public void ChangeStat(string afflicted, float statChange)
    {
        BaseStat stat = FindStat(afflicted);
        float newStatvalue = stat.value + statChange;

        if (newStatvalue > stat.max)
        {
            newStatvalue = stat.max;
        }
        else if(newStatvalue < stat.min)
        {
            stat.value = stat.min;
        }
        else
        {
            stat.value += statChange;
        }
        RpcUpdateSyncFloat(stat.value, afflicted);
    }

    //Sends the updated stats to everyone, the stat has to be in string form so it can find the local stat variable
    [ClientRpc]
    public void RpcUpdateSyncFloat(float statChange, string afflicted)
    {
        FindStat(afflicted).value = statChange;
    }