Unity to Arduino Time Out issue

Hi, i am very new to arduino and unity programming. However, as i was building this project of controlling multiple haptic vibrators through multiplexer within unity i encountered this issue of unity froze (time out after i set writeTimeOUT) every time i call the write to Arduino function. sometimes, it works for the first 2 calls to writeToArduino but stopped working then. I am completely lost on what is causing this problem, would really appreciate the help/

Arduino Code

#include <Wire.h>
#include <SoftwareSerial.h>
#include <SerialCommand.h>
#include <string.h>
#include "Adafruit_DRV2605.h"
#define TCAADDR 0x70

Adafruit_DRV2605 drv[7];
bool hapticOn = true;
uint32_t wait = 1.;
SerialCommand sCmd;
int input[] = {0, 0, 0, 0, 0, 0,0};
void tcaselect(uint8_t i) {
  if (i > 7) return;
 // Serial.println("tcaselect");
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  byte error = Wire.endTransmission();
  //S//erial.println(error);

}
void setup() {
  Serial.begin(9600);
  //while (!Serial);
  //Serial.println("DRV test");
 // Wire.begin();

  
  sCmd.addCommand("index", indexHandler);
  sCmd.addCommand("thumb", thumbHandler);
  sCmd.addCommand("middle", middleHandler);
  sCmd.addCommand("pinky", pinkyHandler);
  sCmd.addCommand("ring", ringHandler);
  sCmd.addCommand("palm", palmHandler);
  sCmd.addCommand("indexOff", indexOffHandler);
  sCmd.addCommand("thumbOff", thumbOffHandler);
  sCmd.addCommand("middleOff", middleOffHandler);
  sCmd.addCommand("pinkyOff", pinkyOffHandler);
  sCmd.addCommand("ringOff", ringOffHandler);
  sCmd.addCommand("palmOff", palmOffHandler);
  sCmd.addCommand("off", offHandler);
  sCmd.addCommand("ON", onHandler);

  

}
void indexHandler(const char *command) {
  hapticOn = true;
  //Serial.println("index activated");
  input[2] = 1;
}

void indexOffHandler(const char *command) {
    hapticOn = true;
  //Serial.println("index Deactivated");
  input[2] = 0;
}
void thumbHandler(const char *command) {
    hapticOn = true;
  //Serial.println("thumb activated");
  input[6] = 1;
}

void thumbOffHandler(const char *command) {
    hapticOn = true;
  //Serial.println("thumb Deactivated");
  input[6] = 0;
}
void palmHandler(const char *command) {
    hapticOn = true;
   //Serial.println("palm activated");
  input[0] = 1;
  input[5] = 1;
}

void palmOffHandler(const char *command) {
  input[0] = 0;
  input[5] = 0;
}

void middleHandler(const char *command) {
    hapticOn = true;
  input[3] = 1;
}

void middleOffHandler(const char *command) {
  input[3] = 0;
}
void pinkyHandler(const char *command) {
    hapticOn = true;
  input[1] = 1;
}

void pinkyOffHandler(const char *command) {
  input[1] = 0;
}
void ringHandler(const char *command) { 
  input[4] = 1;
}

void ringOffHandler(const char *command) { 
  input[4] = 0;
}
void offHandler(const char *command) {
    input[0] = 0;
    input[5] = 0;
}

void onHandler(const char *command) {
   input[0] = 0;
   input[5] = 0;
}

void loop() {
 //Serial.println("looping");
  if (Serial.available() > 0) {
    sCmd.readSerial();
      //Serial.read();
  }
  
    for (uint8_t j = 0; j <= 7; j++) { //For all the 8 ports
      if (input[j] == 1) {
        tcaselect(j);   // Selects only the ports which are available
    
        drv[j].setMode(DRV2605_MODE_INTTRIG); // default, internal trigger when sending GO command
    
        drv[j].selectLibrary(1);
        drv[j].setWaveform(0, 84);  // ramp up medium 1, see datasheet part 11.2
        drv[j].setWaveform(1, 1);  // strong click 100%, see datasheet part 11.2
        drv[j].setWaveform(2, 0);  // end of waveforms
        drv[j].go();
        //Serial.flush();
      }
    }
  
 }

Unity Code

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

/** A class to facilitate the communication between Unity
 *  and the Arduino.
*/
public class ArduinoInterface : MonoBehaviour {

	public string port = "COM3";
	public int baudRate = 9600;

	private SerialPort stream;

	// Use this for initialization
	void Start () {
		stream = new SerialPort (port, baudRate);
		stream.ReadTimeout = 50;
		stream.WriteTimeout = 1000;

		stream.Open ();
	}

	// Writes a string to the Arduino. 
	public void WriteToArduino(string msg) {
		
		//
		//stream.DiscardOutBuffer();
		print ("Writing to Arduino: " + msg);
		try {
			if (stream.IsOpen) {
				//stream.Dispose ();
				//if (!stream.BaseStream.CanRead) print("can't read");
				stream.WriteLine (msg);

				//print ("this is the code");
				stream.BaseStream.Flush ();
			} else {
				//stream.Open();
				print("serial is not open");
			}
		} catch (Exception e) {
			stream.Close ();
			stream.Open ();
		}

	}
}
uint32_t wait = 1.;

Interesting.

Hi Paul, Can you point me to a direction to what might have gone wrong? i am really new to this. Thank you so much for your time

Do you know what the u in uint32_t means?
Do you know what the int in uint32_t means?
Do you know what the 32 in uint32_t means?
Do you know what the _t in uint32_t means?
Do you know why assigning 1.0 to an integer variable is interesting?