Splitting script to send signal to arduino
I have a script that sends a signal to the Arduino serial when a cube hits an object. I have multiple cubes, if one cube hits an object it sends "A" to the serial, and when it doesn't hit it should send a lowercase "a", and the next cube sends "B" and "b", the next "C" and "c" and so on.
However i wanted to make a script that i can just attach to each cube, and give them a number by a variable ("fNum") so i can simply add the script to one cube, give the public variable "fNum" a value of 1 and it will send the signal "A" to the serial when it hits something, and then attach it to the next cube and set the variable in the next cube to 2, and it will send the signal "B" and so on, but i have to split up the code into two scripts because the script i made opens the serial and therefore if i attach it to two different cubes it opens two serials and that doesn't work obviously, but i need help with splitting up the code so i have one script attached to each cube, which acts as a sender, and one script that receives signals from all the other cubes and sends the signals out to the serial so it only opens one connection.
The script i have is:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class collisionDetection : MonoBehaviour {
public SerialPort serial = new SerialPort ("COM4", 9600);
public int fNum;
public bool[] fingers = new bool[]{false, false, false, false };
public bool on = false;
public string[] onOff = new string[]{"A","B","C","D"};
void Start () {
serial.Open ();
}
void Update(){
if (fingers[fNum] == true) {
finger(onOff[(fNum)-1]);
} else {
serial.Write (onOff[fNum-1].ToLower);
on = false;
}
}
void OnCollisionEnter (Collision col)
{
fingers[fNum] = true;
}
void OnCollisionExit(Collision col){
fingers[fNum] = false;
}
public void finger(string fNumString){
if (serial.IsOpen == false)
serial.Open ();
if (on == false) {
serial.Write(fNumString);
on = true;
}
}
}
Also, the line serial.Write (onOff[fNum-1].ToLower);
gives me an error, and i can't figure out how to convert the letter in the array to lowercase.
Any help with this would be greatly appreciated.