Hello,
I have a script that must run a client to compute some value, which must then be sent to another client. This script is attachted to a scene object, which is on the scene from the very beginning, and I do not see any reason to Spawn it at runtime.
I am trying to sync the computed values using both a [Command] to update the value from the first client to the server, and [SyncVar]s to sync the variables to the second client.
The problem is that I have an error telling me “trying to send command for object without authority”.
Here’s my code :
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.UI;
public class myClass : NetworkBehaviour
{
[SyncVar]
private int var1 = 0;
private int var2 = 0;
[SyncVar]
private string var3 = "";
[SyncVar]
private float var4 = 0f;
public override void OnStartServer()
{
// disable client stuff
// switch off the GO if not necessary
if (false) // some test here
{
gameObject.SetActive(false);
}
public override void OnStartClient()
{
if (true) // some test
{
Debug.Log("Will send Asking for Authority");
CmdAssignAuthority();
}
else if (!true) // some test
gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
// Computation
if (true) // some test, including test which client is it
{
var1 = 1;
var2 = 2;
var3 = "3";
var4 = 4;
CmdUpdateVars(var1, var2);
}
}
[Command]
private void CmdUpdateVars(int v1, float v4)
{
var1 = v1;
var4 = v4;
v3 = (test) ? v1 : 0;
}
[Command]
private void CmdAssignAuthority()
{
GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);
Debug.Log("CmdAssignAuthority to " + connectionToClient.hostId);
}
}
The attached NetworkIdentity have localPlayerAuthority set to true.
Thanks in advance !