0

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()
    }
}
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • why would you send an RPC **every frame**? Also if this is running on the client it won't work ... `[ClientRpc]` may only be called by the **server** and is then executed on **all clients** ... could you remove the clutter of commented code a bit and explain what exactly you are trying to achieve? – derHugo Mar 31 '21 at 06:58
  • i'm trying to send the command B from client to the server. but it never goes. when I press b it sends u to the arduino where arduino runs the servo according to this button. but it only happens on the server, for the client i am not able to send anything to the server or from server to the client. – Emperor Lithium Mar 31 '21 at 07:40

0 Answers0