[SyncVar(hook="function")] question

Hi guys,

If I use a hook on my SyncVar, the SyncVar doesn’t update unless I do it manually (mySyncVar = hookValue) ?

1 Like

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.

3 Likes

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

2 Likes

Ok so I might found a bug.

In my code I have this :

    [SyncVar(hook="FindHitPoint")]
    public Transform syncTarget = null;

    private Transform target = null;

    private void FindHitPoint(Transform value)
    {
        //syncTarget = value;
        target = value.FindChild("HitPoint");
    }

    void Update () 
    {
        if (syncTarget != null)
        {
            if (target != null)
            {
                Debug.Log("Test");
            }
        }
     }

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.

1 Like