Class SyncEventAttribute | Multiplayer HLAPI | 1.0.8
docs.unity3d.com
    Show / Hide Table of Contents

    Class SyncEventAttribute

    This is an attribute that can be put on events in NetworkBehaviour classes to allow them to be invoked on client when the event is called on the server.

    [SyncEvent] events are called by user code on UNET servers, and then invoked on corresponding client objects on clients connected to the server. The arguments to the Event call are serialized across the network, so that the client event is invoked with the same values as the function on the server. These events must begin with the prefix "Event".

    using UnityEngine;
    using UnityEngine.Networking;
    
    public class DamageClass : NetworkBehaviour
    {
       public delegate void TakeDamageDelegate(int amount, float dir);
    
       [SyncEvent]
       public event TakeDamageDelegate EventTakeDamage;
    
       [Command]
       public void CmdDoMe(int val)
       {
           EventTakeDamage(val, 1.0f);
       }
    }
    
    public class Other : NetworkBehaviour
    {
       public DamageClass damager;
       int health = 100;
    
       void Start()
       {
           if (NetworkClient.active)
               damager.EventTakeDamage += TakeDamage;
       }
    
       public void TakeDamage(int amount, float dir)
       {
           health -=  amount;
       }
    }

    SyncEvents allow networked actions to be propagated to other scripts attached to the object. In the example above, the Other class registers for the TakeDamage event on the DamageClass. When the event happens on the DamageClass on the server, the TakeDamage() method will be invoked on the Other class on the client object. This allows modular network aware systems to be created, that can be extended by new scripts that respond to the events generated by them.

    Inheritance
    Object
    Attribute
    SyncEventAttribute
    Namespace: UnityEngine.Networking
    Syntax
    [AttributeUsage(AttributeTargets.Event)]
    [Obsolete("The high level API classes are deprecated and will be removed in the future.")]
    public class SyncEventAttribute : Attribute, _Attribute

    Fields

    channel

    The UNET QoS channel that this event should be sent on.

    This defaults to zero - the default reliable channel. This can be used to make events that are not essential for game play (such as effects) unreliable.

    Declaration
    public int channel
    Field Value
    Type Description
    Int32
    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