UNET - syncvars
Hello guys I wanna ask something about unet syncvars.
What is syncvars. How does it works ? Can you give me some guidelines
Answer by JJMGsoftworks · Jan 23, 2017 at 01:01 AM
The best answer for this question is to go straight to the docs Here. If this doesn't answer the question you should try watching brackeys multiplayer tutorial on YouTube or some other up to date source. As for guidelines as with most of the things with code, just try it out.
It's free to run a local multiplayer test to try out networking and you'll likely get a far better idea of how it works in context.
Answer by Oakioats · Mar 05, 2017 at 05:00 AM
Its a way for the networked identity object thats being synced over the server and client to link their variables together. For instance, the "health" variable on a player object. We want that variable to be the same on the server and the client.
This is what it will look like
[SyncVar] public int health;
You can also link a function to be called once the variable changes from server to client. it will look something like this.
[SyncVar( hook = "UpdateHealthUI")] public int health;
void UpdateHealthUI(){ //FUNCTION GUTS HERE }
So whenever the server syncs/updates the variable back to the client the function will be called when it changes.