Slow reading from Unity via Arduino!! URGENT!

Hi guys, I need some help speeding up the readings in Unity 3D. Thus far I am able to read X and Y values in the Arduino through COM8. The problem occurs when I try and read both values in Unity. It reads fast (when I look at the serial monitor in the arduino IDE) but when I press play in Unity there is a tremendous lag between the time the X and Y values change and the time Unity displays/reads those values.

THE ARDUINO CODE(the necessary part):

    int values[] = {round(X),round(Y)};



using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class RightWheel : MonoBehaviour {

public float speed;
public float MoveBy;
public int X = 0;
public int Y = 0;

SerialPort sp = new SerialPort("COM8", 9600);

// Use this for initialization
void Start () {
	sp.ReadTimeout = 1;


// Update is called once per frame
void Update () {
	string value = sp.ReadLine(); //Read the information
    string[] vec3 = value.Split(','); 
	X = int.Parse(vec3[0]);
	Y = int.Parse(vec3[1]);


Alongside what mhtraylor said, your Arduino code is delaying for 10 ms and your Unity code is only waiting for 1 ms. That means your Unity code will timeout 10 times for every time it actually gets data (since you’re using SerialPort.ReadLine() you’ll get a TimeoutException).

If you increase the timeout in Unity to higher, you’ll probably end up freezing Unity until the Arduino sends data, which a bad idea. Especially since 10ms is an eternity to a computer. But, if you’re not too worried, just changing

 sp.ReadTimeout = 1;


 sp.ReadTimeout = 100;

should help you get further.

Alternatively, you could wrap your ReadLine() in a try/catch:

// Update is called once per frame
void Update () {
    string value = sp.ReadLine(); //Read the information
    string[] vec3 = value.Split(','); 
    X = int.Parse(vec3[0]);
    Y = int.Parse(vec3[1]);
catch(TimeoutException) {}

do something async (like mhtraylor suggests), Unity Coroutines might be helpful, pass the data between threads, or just use the SerialPort DataReceived event. Which will call a method every time your Arduino sends data. Example for that event is at: Serial Port DataReceivedEvent

Good luck!

try with this in unity, work for me

if ((cadena=sp.ReadLine()) != null)
                    data = cadena.Split(',');
                    variable= int.Parse(data[0]);
                    volante = int.Parse(data[1]);
                     freno = int.Parse(data[2]);
                     clutch = int.Parse(data[3]);

This is my code for getting six (6) on or off switches from my arduino into unity. The arduino code is sending thestate of 7 switches, sequentially and continuously. Button 9 is a placeholder at the start of the data string, so if SetSwitches() reads anything else first, it’s not the beginning of the string of inputs.

void Update ()
	if (WalkPort.IsOpen) {

		try {

			Button9 = WalkPort.ReadByte ();
			Button2 = WalkPort.ReadByte ();
			Button3 = WalkPort.ReadByte ();
			Button4 = WalkPort.ReadByte ();
			Button5 = WalkPort.ReadByte ();
			Button6 = WalkPort.ReadByte ();
			Button7 = WalkPort.ReadByte ();				

			SetSwitches ();

		} // <<< -------------------------------- END OF TRY

		catch (System.Exception) {


		} // <<< ------------------------------- END OF CATCH

	} // <<< ------------- END OF WalkPort.IsOpen

} // <<< --- END OF UPDATE