Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
Dismiss Notice
Join us now in the Performance Profiling Dev Blitz Day 2023 - Q&A forum where you can connect with our teams behind the Memory and CPU Profilers and the Frame Debugger.

Unity Multiplayer [SyncVar(hook="function")] question

Discussion in 'Multiplayer' started by xXApOXx, Jul 16, 2015.

  1. xXApOXx


    Feb 9, 2015
    Hi guys,

    If I use a hook on my SyncVar, the SyncVar doesn't update unless I do it manually (mySyncVar = hookValue) ?
    Last edited: Jul 16, 2015
    AbhinayNegi likes this.
  2. Deleted User

    Deleted User


    You update the SyncVar variable on the server, either through a Command or in a script that runs on a gameobject that has a NetworkIdentity (check if isServer if not a server only object). When the SyncVar changes on the server, the server then updates it on all clients.

    The hook is called on the client right after the SyncVar is updated.
    AbhinayNegi, Caspella and dev99pro like this.
  3. Vanamerax


    Jan 12, 2012
    Correct that to 'right before the Syncvar is updated'. You will get the new value as a parameter in the hook. You will have to apply this value manually to the original member. (eg overwrite the old value that is still there)

    This will allow for custom logic based on old and new value since you can still read the old value from the field
    AbhinayNegi and Deleted User like this.
  4. xXApOXx


    Feb 9, 2015
    Ok so I might found a bug.

    In my code I have this :

    Code (csharp):
    2.     [SyncVar(hook="FindHitPoint")]
    3.     public Transform syncTarget = null;
    5.     private Transform target = null;
    7.     private void FindHitPoint(Transform value)
    8.     {
    9.         //syncTarget = value;
    10.         target = value.FindChild("HitPoint");
    11.     }
    13.     void Update ()
    14.     {
    15.         if (syncTarget != null)
    16.         {
    17.             if (target != null)
    18.             {
    19.                 Debug.Log("Test");
    20.             }
    21.         }
    22.      }
    If I don't uncomment "//syncTarget = value;" in my "FindHitPoint" function, the Update doesn't reach the log on clients... I checked syncTarget and it's still null on clients.

    By the way, if I uncomment "//syncTarget = value;" in "FindHitPoint", the function isn't recall by the assignation of value at syncTarget.
    alimoeeny likes this.