Unity to Arduino

Is there a way to send data from Unity to Arduino and back?

COM4 is for windows. For Mac - change " COM4" to the full serial port name for mac. Mine is “/dev/tty.usbmodem1421”

Here is an example I’ve been working with.
This is an example C# code for Unity:

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

public class SerialPortTest : MonoBehaviour 
{
    //Setup parameters to connect to Arduino
	public static SerialPort sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
	public static string strIn;    	
	
	// Use this for initialization
	void Start () 
	{
	OpenConnection();
	}

	void Update()
	{
            //Read incoming data
		strIn = sp.ReadLine();
	print(strIn);

           //You can also send data like this
           //sp.Write("1");


	}
	
	//Function connecting to Arduino
	public void OpenConnection() 
	{
		if (sp != null) 
		{
			if (sp.IsOpen) 
			{
				sp.Close();
				message = "Closing port, because it was already open!";
			}
			else 
			{
				sp.Open();  // opens the connection
				sp.ReadTimeout = 50;  // sets the timeout value before reporting error
				message = "Port Opened!";
			}
		}
		else 
		{
			if (sp.IsOpen)
			{
				print("Port is already open");
			}
			else 
			{
				print("Port == null");
			}
		}
	}

	void OnApplicationQuit() 
	{
		sp.Close();
	}
}

This is an example code for the Arduino:

void setup() 
{
  Serial.begin(9600);
}

void loop() {
 int val = 2;

//Sending value
 Serial.write(val);
 delay(1000);

//Receiving value
 if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();

		// say what you got:
		Serial.print("I received: ");
                Serial.println(incomingByte, DEC);

}

Hope this helps :wink:

If you are on a Mac you can use the following example will oscP5 to send and recieve data between Arduino and Unity: http://www.sundh.com/blog/2012/05/unity-processing-arduino/

The Kinect2Arduino Package does just that; it can send serial messages to and from the Arduino and Unity. It’s available in the asset store here: https://www.assetstore.unity3d.com/#/content/9680