Arduino to unity

I have got a script to send data from arduino to unity but it doesn´t work. When i open the serial monitor of arduino, all data are ok, but in unity it doesnt work… When i click on play in unity the Rx led of arduino blink, but that´s it, no data from arduino. I need some help please!!!

My arduino code:

const int buttonPin01 = 6;
const int buttonPin02 = 7;

void setup()
{
  Serial.begin(9600);
  
  pinMode(buttonPin01, INPUT);
  pinMode(buttonPin02, INPUT);
  digitalWrite(buttonPin01, HIGH);
  digitalWrite(buttonPin02, HIGH);
}

void loop()
{
  if(digitalRead(buttonPin01) == LOW)
  {
  Serial.print(1);
  Serial.flush();
  delay(20);
}

if(digitalRead(buttonPin02) == LOW)
  {
  Serial.print(2);
  Serial.flush();
  delay(20);
}
}

And my unity code:

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


public class TutScript : MonoBehaviour {
	
	public float speed;
	private float amountToMove;
	
	private SerialPort sp = new SerialPort("COM3",9600);
	 
	// Use this for initialization
	void Start () {
		sp.Open();
		sp.ReadTimeout = 0;
	
	}
	
	// Update is called once per frame
	void Update () {
	
		amountToMove = speed * Time.deltaTime;
		
		if(sp.IsOpen)
		{
			try
			{
				MoveObject(sp.ReadByte());
				print(sp.ReadByte());
			}
			catch(System.Exception)
			{
			}
		}
	}

	
	void MoveObject(int Direction) 
{
	if (Direction == 1)
	{
		transform.Translate(Vector3.left * amountToMove, Space.World);
	}
	if (Direction == 2)
	{
		transform.Translate(Vector3.right * amountToMove, Space.World);
	}
}
	}

Hi, I had the same problem this sp.Open(); sp.ReadTimeout = 0;

should never be 0, you have to timeout for at least 1 milisecond. If you wan’t it to continiously read, just put // infront of that code to disable it or remove the line.

hey guys hope you can help me!
i try to use the serial numbers from my arduino board to transform the object in unity. on arduino site my code is really simple and looks like this:

  void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int a = analogRead(A0);
  //Sleep for 50ms, which provides the recommended sample rate (20Hz)
  delay(500);
  Serial.println(a);
}

in the serial monitor from arduino i get these values (picture)

now i want to use these values to constantly transform my geometry (a cube) in unity. for example if the value from arduino is 50 so the cube should be scaled by 50

do you know how this can work???

thanks in advance

What namespaces are you using?
using System.IO is obsolete right now and changing my API compatibility to .NET 2.0 doesn’t change anything.