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