Send information from unity to arduino

Hello. I have this in Unity

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

public class Juego : MonoBehaviour {

	SerialPort puerto=new SerialPort("COM3",9600);
	
	void Start () 
	{
		puerto.Open();

	}
	
	
	void Update () 
	{
		puerto.Write ("2");
	}
}

when I run Unity I have an error "InvalidOperationException: Specified port is not open."

In arduino I have this

int test = 8 ;


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(test,OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  int unity= Serial.read();
  if(unity==2)
  {
    digitalWrite(prueba,HIGH );  
  }
  else
  {
    digitalWrite(prueba,LOW ); 
  }
}

Unity should send "2" and then arduino read this and turn on the led. But I dont know how works this. any help please? thanks.

Is the Arduino connected to COM3? Is any other application, like the Serial Monitor, also using COM3?

Since you are sending "2", that is NOT going to equal 2 on the Arduino. One of the characters in the string will match '2'.

yes, its conected to COM3.

In unity with write() function I need to send a string. but in arduino needs read an int... Im lost

but in arduino needs read an int...

The Serial.read() function needs to return an int, because some people do not bother checking that there is anything to read before trying to read.

If everyone did, then Serial.read() would return a char or a byte. To read a string, you need to read and store multiple letters, in a NULL terminated array of chars. You could then use atoi() to convert the string to an int. But, in order to do that, you need to know when the end of packet arrives. Nothing in what you are sending says when the packet is done.

What causes the Start function to be called?

Your Unity program will need to allow time for the Arduino to reset after it opens the serial port and before it sends any data.

The examples in serial input basics are simple reliable ways for the Arduino to receive data. There is also a parse example.

...R

I dont understand what to do. Any example with unity?

I made it work, but work sometimes.

Unity code

SerialPort puerto=new SerialPort("COM3",9600);

.
.
.
void Start () 
	{
		puerto.Open();
		

	}

void OnTriggerEnter2D(Collider2D other)
{
    if(other.gameobject.tag=="red")
         if(puerto.IsOpen==false)
				{
					puerto.Open();
					puerto.Write ("r");
				}
          puerto.Write ("r");
   
    else if (other.gameobject.tag=="green")
    {
                if(puerto.IsOpen==false)
				{
					puerto.Open();
					puerto.Write ("g");
				}
          puerto.Write ("g");
     }

     else 
    {
                if(puerto.IsOpen==false)
				{
					puerto.Open();
					puerto.Write ("b");
				}
          puerto.Write ("b");
     }

}

A ball with the script, touch bricks of different colors, red,green and blue, when touch this, send the message to arduino. the problem is the 2 first impacts, dont work, then yes. And if the ball touches many bricks, or too fast, dont work.. why?

Arduino code

int red = 9;
int blue = 8;
int green = 11;

void setup() {
 
  Serial.begin(9600);
 
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}


void loop() {
  
  String unity = Serial.readString();
  
  if (unity=="r")
  {
    digitalWrite(red,HIGH);
  }
  else if(unity=="b")
  {
    digitalWrite(blue,HIGH);
  }
  else if(unity=="g")
  {
    digitalWrite(green,HIGH);
  }
  else
  {
    digitalWrite(green,LOW);
    digitalWrite(blue,LOW);
    digitalWrite(red,LOW);
  }
}

What I need to change? thanks

  String unity = Serial.readString();

You are sending one character. There is no earthly reason to use a blocking function to read one character. There is no reason to store one character in a String.

PaulS:

  String unity = Serial.readString();

You are sending one character. There is no earthly reason to use a blocking function to read one character. There is no reason to store one character in a String.

what I need to do then?

what I need to do then?

if(Serial.available() > 0)
{
   char ltr = Serial.read();
   if(ltr == 'r')
   {
   }
   else if(ltr = 'g')

PaulS:

if(Serial.available() > 0)

{
  char ltr = Serial.read();
  if(ltr == 'r')
  {
  }
  else if(ltr = 'g')

ok thanks, I try it.