Search Unity

[SyncVar(hook="function")] question

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

  1. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    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

    Guest

    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

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    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

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Ok so I might found a bug.

    In my code I have this :

    Code (csharp):
    1.  
    2.     [SyncVar(hook="FindHitPoint")]
    3.     public Transform syncTarget = null;
    4.  
    5.     private Transform target = null;
    6.  
    7.     private void FindHitPoint(Transform value)
    8.     {
    9.         //syncTarget = value;
    10.         target = value.FindChild("HitPoint");
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         if (syncTarget != null)
    16.         {
    17.             if (target != null)
    18.             {
    19.                 Debug.Log("Test");
    20.             }
    21.         }
    22.      }
    23.  
    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.