I'm trying to make an app where I send the input from my android device to the server i.e. my pc where Arduino is connected. So the Pc(server) sends the input to the Arduino. But using Mirror, i am having slight difficulties regarding the syncing of data. I'm able to connect both the server and the client but I'm not able to send the data from client to server or from server to client. I have tried the YouTube tutorials and the [Command] too but I think I'm missing something. I just want to send command from the client to server so it sends to Arduino. If anyone can help, it will be a great deal. The code for script is as
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
public class syncData : NetworkBehaviour
{
public GameObject DFA;
public TMP_Text data1;
public TMP_Text data2;
public TMP_Text data3;
[SyncVar]
string receivedString;
public delegate void StringChangedDelegate(string receivedString);
/*[SyncEvent]*/
public event StringChangedDelegate EventStringChanged;
private void SetString(string receivedString)
{
receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
}
public override void OnStartServer()
{
SetString(receivedString);
}
// Start is called before the first frame update
void Start()
{
// DFA = GameObject.FindWithTag("dfa"); // tag, not name
}
/* [Command]
private void changeString() => SetString(receivedString);
*/
// Update is called once per frame
// [ClientCallBack]
void Update()
{
// Debug.Log("Client is sending information.");
// receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
refresh(receivedString);
}
[Server]
void FunctionServer()
{
Debug.Log("Running the program in Server mode.");
}
[ClientRpc]
void refresh(string receivedString)
{
// receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.B))
{
Debug.Log(" Right Arrow Button was pressed on Vuzix");
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.X))
{
Debug.Log(" Left Arrow Button was pressed on Vuzix");
}
refresher();
// string[] datos = receivedString.Split(':'); //My arduino script returns a 3 part value (IE: 12,30,18)
//// data1.SetText(datos[0]);// Try to forcefully re-enter values from DFA
// data2.SetText(datos[1]);
// data3.SetText(datos[2]);
}
[Command]
void refresher()
{
// receivedString = DFA.GetComponent<Comunicacion>().receivedstring;
refresh(receivedString);
Debug.Log("A command has been sent");
// gm.Update()
}
}