Receiving duplicate values

I am getting values over udp protocol…I am able to receive values from other script but the values are getting replicated…i don’t know why.

Hat.cs:

     void FixedUpdate () {
       
        //Camera camera = GetComponent<Camera>();
        //Vector3 originalPosition = cam.ScreenToWorldPoint(Input.mousePosition);
        //Vector3 originalPosition = cam.ScreenToWorldPoint(Vector3.zero);
        GameObject HatSprite = GameObject.Find ("HatSprite");
        UDPDualReceive direction = HatSprite.GetComponent<UDPDualReceive>();

        ReceiveLeft = direction.lastReceivedLeft;
        ReceiveRight = direction.lastReceivedRight;

        print ("Left:" + ReceiveLeft);
        print ("Right:" + ReceiveRight);
    }

UDPDualReceive.cs:

public byte[] data1,data2;


private void init(int port)
{
    LeftData= new Thread(new ThreadStart(ReceiveLeftData));
     RightData = new Thread(new ThreadStart(ReceiveRightData));
}
private void ReceiveLeftData()
    {

        client1= new UdpClient(port1);

        while (true)
        {

            try
            {
                // Bytes empfangen.
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61556);
                 data1 = client1.Receive(ref anyIP);
              

                // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                string text1 = Encoding.UTF8.GetString(data1);
                 // latest UDPpacket
                //print("data:"+text1);
                lastReceivedLeft=text1;
            }
            catch (Exception err)
            {
                print(err.ToString());
            }

        }
        lastReceivedLeft="";
    }


private void ReceiveRightData()
    {
       
        client2 = new UdpClient (port2);
        while (true)
        {

            try
            {
                // Bytes received.
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 61558);
                 data2 = client2.Receive(ref anyIP);

                string text2 = Encoding.UTF8.GetString(data2);
                lastReceivedRight = text2;
                //print("data2:"+text2);
                Array.Clear(data2,0,data2.Length);
            }
            catch (Exception err)
            {
                print(err.ToString());
            }

        }
        lastReceivedRight="";

What’s repeated? left and right?

Yes i have verified in UDPDualReceive values received are unique but if i print these values in hat.cs i am getting same left and right values about 10 times…

Ahh okay. Well, here’s a thought on that.
Do you think that maybe they’re printing the same value ~ 10 times each, because the value hasn’t changed at all; you’re just spitting out the same text over and over (seeing the same thing) until it does change later and you see new values?

yes exactly…

Just to be clear, when you say “yes, exactly”… does that mean you fixed it? :slight_smile: