Trying to figure out why my variables aren't syncing from the server to the client. I've watched them change on the server...but they're not changing on the client. Code (CSharp): usingUnityEngine; usingUnityEngine.Networking; usingSystem.Collections; public class Input : NetworkBehaviour { private bool cameraOn; private Player player; void Awake() { player = GetComponent<Player>(); } void Update() { if(!isLocalPlayer) return; if(!cameraOn) { Camera.main.SendMessage("SetTarget", transform); cameraOn = true; } if(Input.GetKeyDown(KeyCode.Q)) player.CmdLastMove(); if(Input.GetKeyDown(KeyCode.E)) player.CmdNextMove(); } Code (CSharp): using UnityEngine; using UnityEngine.Networking; using System.Collections; using System.Collections.Generic; public class Player : NetworkBehaviour { public List<Move> LearnedMoves = new List<Move>(), KnownMoves; [SyncVar(hook="SelectMove")] public int activeMoveIndex; public Move activeMove; [Command] public void CmdNextMove() { if(activeMoveIndex == KnownMoves.Count - 1) { activeMoveIndex = 0; } else { activeMoveIndex++; } activeMove = KnownMoves[activeMoveIndex]; } [Command] public void CmdLastMove() { if(activeMoveIndex == 0) { activeMoveIndex = KnownMoves.Count - 1; } else { activeMoveIndex--; } activeMove = KnownMoves[activeMoveIndex]; } public void SelectMove(int _activeMoveIndex) { activeMoveIndex = _activeMoveIndex; activeMove = KnownMoves[activeMoveIndex] }
BUMP ... A more detailed explination: If I run the host inside Unity and the client in the application... In the client if I press Q or E I can see that in the inspector inside Unity (the host) the variables are changing. So the commands are indeed being sent to the host/server. If I then run the client inside Unity and the host in the application... In the client (which is now Unity) if I press Q or E I see that the variables are not changing in the inspector. So the SyncVars aren't actually syncing from the server to the client. The GameObject has the following attached to it, in this order: Transform Capsule Collider Rigidbody Animator Network Identity (Local Player Authority) Network Animator Player (Custom Script) Input (Custom Script) Attack One (Custom Script) Attack Two (Custom Script) Network Transform
If the value is changing on the server, but not synchronizing on the client my best guess is that maybe it's not setting dirty bit properly. You can troubleshoot this 2 ways. either... -Strip code down to bar bones (without network animator, network transform, etc). I know those additional components had some bugs affecting syncVars. Just have your Network Identiy and NetworkBehavior script, nothing else. See if this resolves issue. -Manually SetDirtyBit( ) to see if it synchronizes to client. Just some things to try.
it looks like it should work. You could put a Debug.LogError() in your SelectMove() function to check if it is really being called.. or try a debugger. public void SelectMove(int _activeMoveIndex) { Debug.LogError("SelectMove for " + netId + " index:" + _activeMoveIndex); activeMoveIndex = _activeMoveIndex; activeMove = KnownMoves[activeMoveIndex] }
Similar issue here. Am I not getting how SyncVars are supposed to work? Why can't I use "buildProgress" instead of the "value" in the OnBuildProgressChanged function? buildProgress is remaining what it was when joining. The slider value in the hook function will only match the "value" sent, but won't read the server's "buildProgress". The SyncVar is not being read by the clients. Edit: I think this is the issue - http://issuetracker.unity3d.com/issues/syncvar-hooks-on-host-dont-have-access-to-old-value http://forum.unity3d.com/threads/ne...s-dont-match-expectation.334890/#post-2196522 http://forum.unity3d.com/threads/syncvar-hook-function-question.341374/#post-2208714 Member variable values won't update unless you apply them in the hook function, so you just have to apply it manually.
I believe the issue is due to your local player authority. I believe it said somewhere in some docs that this is what determines which direction the SyncVar goes. If its Local Player Authority it goes from Player -> Server otherwise its Server -> Player In case you are wondering, I believe most of the HLAPI components act in the same way such as NetworkTransform, etc.
SyncVar hooks functions are passed the new value of the variable, and it is up to your code to apply that value to the local member variable. Code (CSharp): void OnBuildProgressChanged(float value) { Debug.Log("diff = " + (value -buildProgress)); buildProgress = value; } This allows your code to calculate the difference between the old and new values.
I... just found this now... and I thank you... I have been setting it in all sorts of squirly ways...
Hi, I have same issue. The SyncVar from Host isn't synching to other client. And the SyncVar Attribute is not shown in the inspector. What does that mean?
I have a similar issue, my client doesn't get the updated variables no matter what I do, I tried every possible variation that exists, I even tried to send them as messages using _client.send and then replying with the server and assigning it to another local variable but no matter what magic I use the variable always displays it's content in the inspector but if i try to use it on a client side function it's empty. I have even posted on 3 different sites my code between the 50th and 100th attempt but 0 response. After like 60 hours I'm wondering if I'm retarded or is this completely broken...
It could be a bug, so I try to write another script to reproduce it. Because Syncvar in other scripts works fine.
Another questions: what will the behaviour of a SyncVar be if no authority on NetworkIdentity is checked? Will it still be synched or do I have to do it manually via hook? Does an Object with "Server Only" option execute Client functions? And does a SyncVar needs to be public?
so if no hook function is used it will sync automatically, but if hook function is used you have to manually set the variable to the value passed by hook function. I wish I saw this post earlier....I'v been looking for solution for days!