Using Arduino Ultrasnic

Hi, I’m using an ultrasonic sensor and I need the distance from the arduino in my game, something like an if (distance <= 20){ move = true}, basically use the distance to set true a boolean from another script, and I really don’t know how to do it (the distance part). I can see in my console the distance values but I don’t know how to use them. This is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class ConexionArduino : MonoBehaviour
{
SerialPort serialPort = new SerialPort(“COM3”, 9600);
public bool moverIzq;

void Start()
{
serialPort.Open();
serialPort.ReadTimeout = 10;

}
void Update()
{
try
{
if (serialPort.IsOpen) {
print(serialPort.ReadLine());
int dist = (int.Parse(serialPort.ReadLine()));
if (dist >= 20)
{
moverIzq = true;
}
//MoverTrompa(int.Parse (serialPort.ReadLine()));
}
} catch (System.Exception ex)
{
ex = new System.Exception();
}
}

}

you are not setting moverIzq false anywhere, so try with

if (dist >= 20)
{
moverIzq = true;
}else{
moverIzq = false;
}

or can use shorthand version (which will set it to true or false)

moverIzq = (dist >= 20);

then to use that bool value from another script, use something like

// in another script
public ConexionArduino arduino; // drag your gameobject here with that ConexionArduino script

void Update()
{
  Debug.Log(arduino.moverIzq);
}