Class SyncVarAttribute | Package Manager UI website
docs.unity3d.com
    Show / Hide Table of Contents

    Class SyncVarAttribute

    [SyncVar] is an attribute that can be put on member variables of NetworkBehaviour classes. These variables will have their values sychronized from the server to clients in the game that are in the ready state.

    Setting the value of a [SyncVar] marks it as dirty, so it will be sent to clients at the end of the current frame. Only simple values can be marked as [SyncVars]. The type of the SyncVar variable cannot be from an external DLL or assembly.

    using UnityEngine;
    using UnityEngine.Networking;

    public class Ship : NetworkBehaviour { [SyncVar] public int health = 100;

    [SyncVar] public float energy = 100; }

    The allowed SyncVar types are:

    • Basic type (byte, int, float, string, UInt64, etc)
    • Built-in Unity math type (Vector3, Quaternion, etc),
    • Structs containing allowable types.

    Inheritance
    System.Object
    System.Attribute
    SyncVarAttribute
    Inherited Members
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Boolean)
    System.Attribute.IsDefined(System.Reflection.MemberInfo, System.Type)
    System.Attribute.IsDefined(System.Reflection.MemberInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type)
    System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.ParameterInfo)
    System.Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Type)
    System.Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.ParameterInfo, System.Boolean)
    System.Attribute.IsDefined(System.Reflection.ParameterInfo, System.Type)
    System.Attribute.IsDefined(System.Reflection.ParameterInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttribute(System.Reflection.ParameterInfo, System.Type)
    System.Attribute.GetCustomAttribute(System.Reflection.ParameterInfo, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.Module, System.Type)
    System.Attribute.GetCustomAttributes(System.Reflection.Module)
    System.Attribute.GetCustomAttributes(System.Reflection.Module, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.Module, System.Type, System.Boolean)
    System.Attribute.IsDefined(System.Reflection.Module, System.Type)
    System.Attribute.IsDefined(System.Reflection.Module, System.Type, System.Boolean)
    System.Attribute.GetCustomAttribute(System.Reflection.Module, System.Type)
    System.Attribute.GetCustomAttribute(System.Reflection.Module, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Type)
    System.Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Type, System.Boolean)
    System.Attribute.GetCustomAttributes(System.Reflection.Assembly)
    System.Attribute.GetCustomAttributes(System.Reflection.Assembly, System.Boolean)
    System.Attribute.IsDefined(System.Reflection.Assembly, System.Type)
    System.Attribute.IsDefined(System.Reflection.Assembly, System.Type, System.Boolean)
    System.Attribute.GetCustomAttribute(System.Reflection.Assembly, System.Type)
    System.Attribute.GetCustomAttribute(System.Reflection.Assembly, System.Type, System.Boolean)
    System.Attribute.Equals(System.Object)
    System.Attribute.GetHashCode()
    System.Attribute.Match(System.Object)
    System.Attribute.IsDefaultAttribute()
    System.Attribute.System.Runtime.InteropServices._Attribute.GetTypeInfoCount(System.UInt32)
    System.Attribute.System.Runtime.InteropServices._Attribute.GetTypeInfo(System.UInt32, System.UInt32, System.IntPtr)
    System.Attribute.System.Runtime.InteropServices._Attribute.GetIDsOfNames(System.Guid, System.IntPtr, System.UInt32, System.UInt32, System.IntPtr)
    System.Attribute.System.Runtime.InteropServices._Attribute.Invoke(System.UInt32, System.Guid, System.UInt32, System.Int16, System.IntPtr, System.IntPtr, System.IntPtr, System.IntPtr)
    System.Attribute.TypeId
    System.Object.ToString()
    System.Object.Equals(System.Object, System.Object)
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.GetType()
    System.Object.MemberwiseClone()
    Namespace: UnityEngine.Networking
    Syntax
    [AttributeUsage(AttributeTargets.Field)]
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class SyncVarAttribute : Attribute, _Attribute

    Fields

    hook

    The 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.

    //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); } }

    Declaration
    public string hook
    Field Value
    Type Description
    System.String
    In This Article
    • Fields
      • hook
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023