# Send information from unity to arduino

**URL:** <https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703>\
**Category:** Programming\
**Created:** [April 27, 2015, 1:56pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703 "2015-04-27T13:56:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2015, 1:56pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/1 "2015-04-27T13:56:22Z")

</div>

Hello. I have this in Unity

```cpp
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

```cpp
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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2015, 2:04pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/2 "2015-04-27T14:04:39Z")

</div>

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'.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2015, 2:19pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/3 "2015-04-27T14:19:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2015, 3:01pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/4 "2015-04-27T15:01:19Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [April 27, 2015, 4:18pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/5 "2015-04-27T16:18:23Z")

</div>

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](http://forum.arduino.cc/index.php?topic=288234.0) are simple reliable ways for the Arduino to receive data. There is also a parse example.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 27, 2015, 6:46pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/6 "2015-04-27T18:46:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 28, 2015, 5:52pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/7 "2015-04-28T17:52:36Z")

</div>

I made it work, but work sometimes.

Unity code

```cpp
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

```cpp
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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 28, 2015, 8:34pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/8 "2015-04-28T20:34:22Z")

</div>

```cpp
  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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 28, 2015, 8:39pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/9 "2015-04-28T20:39:48Z")

</div>

> PaulS:
> 
> ```arduino
>   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?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 28, 2015, 9:33pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/10 "2015-04-28T21:33:56Z")

</div>

> what I need to do then?

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 4, 2015, 7:29pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/11 "2015-05-04T19:29:48Z")

</div>

> PaulS:
> 
> ```arduino
> if(Serial.available() > 0)
> 
> ```

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

> ```arduino
> 
> ```

ok thanks, I try it.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:15pm UTC](https://forum.arduino.cc/t/send-information-from-unity-to-arduino/307703/12 "2021-05-05T21:15:38Z")

</div>

Closed on May 5, 2021, 9:15pm UTC


