title: C# SerialPort EventHandler in Unity
body: <p>I am writing C# code for Unity. Just reading SerialPort value in EventHandler. The problem is that the handler is not called. here's the code </p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  using System;
  using System.IO.Ports;
  
  public class MainScript : MonoBehaviour {
  
  public SerialPort mySerialPort;
  public static float speed=100;
  GameObject cube ;
  public GUIStyle style ;
  // Use this for initialization
  void Start () {
  
      cube = GameObject.FindGameObjectWithTag ("cube");
  
      if(mySerialPort.IsOpen)
          mySerialPort.Close();
  
      mySerialPort = new SerialPort("com5");
      mySerialPort.BaudRate = 9600;
      mySerialPort.Parity = Parity.None;
      mySerialPort.StopBits = StopBits.None;
      mySerialPort.DataBits = 8;
      mySerialPort.Handshake = Handshake.None;
      mySerialPort.DataReceived += new SerialDataReceivedEventHandler (DataReceivedHandler);
  
      if(mySerialPort.IsOpen == false)
          mySerialPort.Open();
  }
  
  
  void OnGUI(){
      GUI.Box (new Rect(100,100,100,100),"Speed : " + speed , style);
  }
  // Update is called once per frame
  void Update () {
  // speed = mySerialPort.ReadTo ("\r");
  // update My View with the speed new value
  
      cube.transform.Rotate(Vector3.up * speed *Time.deltaTime);
  
  }
  
  
  public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
  {
      SerialPort sp = (SerialPort)sender;
      speed = float.Parse( sp.ReadTo ("\r") ) ;
      print ("Data Recieved : " + speed);
  }
  }
  </code></pre>
  
  <p>the problem is not in the serial port as, when i read it in the update function for unity it reads the right value but, there's a performance issue updating the UI.</p>
  
  <p>thanks</p>