Select your preferred scripting language. All code snippets will be displayed in this language.
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseThe hook attribute can be used to specify a function to be called when the sync var changes value on the client.
This ensures that all clients receive the proper variables from other clients.
no example available in JavaScript
//Attach this to the GameObject you would like to spawn (the player). //Make sure to create a NetworkManager with an HUD component in your Scene. To do this, create a GameObject, click on it, and click on the Add Component button in the Inspector window. From there, Go to Network>NetworkManager and Network>NetworkManagerHUD respectively. //Assign the GameObject you would like to spawn in the NetworkManager. //Start the server and client for this to work.
//Use this script to send and update variables between Networked GameObjects using UnityEngine; using UnityEngine.Networking;
public class Health : NetworkBehaviour { public const int m_MaxHealth = 100;
//Detects when a health change happens and calls the appropriate function [SyncVar(hook = "OnChangeHealth")] public int m_CurrentHealth = m_MaxHealth; public RectTransform healthBar;
public void TakeDamage(int amount) { if (!isServer) return;
//Decrease the "health" of the GameObject m_CurrentHealth -= amount; //Make sure the health doesn't go below 0 if (m_CurrentHealth <= 0) { m_CurrentHealth = 0; } }
void Update() { //If the space key is pressed, decrease the GameObject's own "health" if (Input.GetKey(KeyCode.Space)) { if (isLocalPlayer) CmdTakeHealth(); } }
void OnChangeHealth(int health) { healthBar.sizeDelta = new Vector2(health, healthBar.sizeDelta.y); }
//This is a Network command, so the damage is done to the relevant GameObject [Command] void CmdTakeHealth() { //Apply damage to the GameObject TakeDamage(2); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!