- Home /
uNet sync issue - Beginner's problem
Hi everbody,
I'm new to uNet and I'm having problems with syncing variables to the host. I have updated Unity to the latest patch, followed the UNET tutorial and read the documentation, but I'm sure something slipped by and I haven't gotten this code to run. I'm trying to get both the host and a client to control a single variable.
The code is very simple, every time I do a right click the value is increased by 1; when I do a left click it should be synced up. Here's the code
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class NetworkTestScript : NetworkBehaviour {
[SyncVar(hook = "HookTest1")]
private int testValue1;
// I just use this variable to make the sync variables visible in the inspector
public int showTest1;
public int myValue;
// initializing with some number
void Awake()
{
testValue1 = -90000;
}
// the controls
void Update()
{
if (isLocalPlayer) {
if (Input.GetMouseButtonUp (0)) {
UpdateTheData();
}
if(Input.GetMouseButtonUp(1))
{
myValue ++;
}
}
showTest1 = testValue1;
}
void UpdateTheData()
{
TransmitValue ();
}
[Command]
void CmdGiveStringToServer(int _myVal)
{
Debug.Log (" my name is "+name+ " I am the Server: "+ isServer+" I am local: "+ isLocalPlayer +" the value recieved is "+ _myVal);
testValue1 = _myVal;
Debug.Log ("Values are now "+ testValue1);
}
[ClientCallback]
void TransmitValue()
{
Debug.Log ("Transmitting from "+ name +" the value of "+ myValue);
CmdGiveStringToServer( myValue);
}
//this just lets me know when it was modified.
void HookTest1(int _var)
{
Debug.Log ("the test1 variable has been synced to "+ _var);
}
}
However, even after the hook is activated, the value in the server does not change while this appeared in the command line, the inspector still showed a value of 2.
when I tried updating the value from the client this happened:
Notice how both objects say they are the server. The value updated but only in that object, not syncing with the host. Likewise, when I change the value of the host, it does not change in the client.
I know I must be forgetting something really simple, but I can't place a finger on it. Do you guys know what I might be doing wrong?
Did you try to assign manually testValue1 in HookTest1? Sth. like: testValue1 = _var;
Hi Erddoy! Thanks for your response. Unfortunately the result is the same. The value only changes on its own script, not on the others. It's almost as if it were treating both as servers.
Just as additional information, the script is attached to the player character and spawned using the Network manager player prefab.
Answer by ElChileVengador · Sep 15, 2015 at 02:53 AM
Found the answer; so the code is actually working fine; it is the way that I was reading it was wrong. I was expecting the [syncVar] to change across all instances of the script, not just the one in that particular gameobject. Thanks to everybody who read the question.
Your answer
Follow this Question
Related Questions
UNET Server not setting [SyncVar] 0 Answers
Networking Sync SetActive Not Working 0 Answers
How to sync network 0 Answers
How fast do Syncvars synchronize? 1 Answer
How to Sync List 1 Answer