- Home /
UNET Server not setting [SyncVar]
Hello all!
Having a problem, consulted with some others and I can't figure this out.
I am using a Command to try to set a variable which should be synced, however the server is not setting the variable when I use it as a dedicated server (no player). When I host one 1 client, and open another it works.
On the player object I have a script, and in it, it contains this code:
[Client]
void UpdateClient_Harvesting()
{
if (Input.GetMouseButtonDown(0) && !Utils.IsCursorOverUserInterface() && Input.touchCount <= 1)
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var harvestNode = hit.transform.GetComponent<HarvestNode>();
var distance = Vector3.Distance(this.transform.position, hit.transform.transform.position);
if (distance > interactionRange)
return;
if (harvestNode)
{
harvestNode.CmdStartHarvest(); // <----- It calls the command successfully
}
}
}
}
You can see there where 'harvestNode.CmdStartHarvest()' is called. Below that I added a debug which came through correctly.
On another object (both have NetworkIdentity's but their Authority is not set as the Server controls them), I have this:
[SyncVar(hook = "OnHarvesting")]
public bool harvesting;
[Command(channel = Channels.DefaultUnreliable)]
public void CmdStartHarvest() // Sent when the player starts to harvest the node
{
harvesting = true;
}
void OnHarvesting(bool b)
{
if(isClient || isLocalPlayer)
{
if(b)
{
harvestBar.activeTimer = b;
_animator.SetInteger("Profession", (int)harvestType);
} else
{
harvestBar.activeTimer = b;
_animator.SetInteger("Profession", 0);
}
}
}
Now this works on Host/Client but if I run the server as Dedicated and connect the Client, the server does not set Harvesting = true;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Network call Method from Instants of Prefab 2 Answers
,How can I resolve the error “Trying to send command for object without authority.”? 1 Answer
Problem numbering Clients 0 Answers
How to find the time between last and current sync in a networked multiplayer? 0 Answers