I'm currently trying to connect the Arduino Nano 33 IoT board to Unity.
This code simply makes the GameObject vanish if Unity reads something from the board.
While coding, there are some SerialPort problems, and I've tried a few solutions like adjusting the API compatibility Level in Unity or something else, but it ain't worked for me as well...
The following is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
public class Nano33IoT : MonoBehaviour
{
public GameObject nano33IoT;
SerialPort sp = new SerialPort("usbmodem11101", 9600);
float m;
void Start()
{
sp.Open();
sp.ReadTimeout = 1;
}
void FixedUpdate()
{
if (sp.IsOpen)
{
try
{
string value = sp.ReadLine();
if (value != "")
{
Debug.Log("Value: " + value);
nano33IoT.SetActive(false);
}
}
catch (TimeoutException)
{
throw;
}
}
}
void Update()
{
}
}
Things just don't work as expected, there are 2 problems that I encountered:
If I switch the API compatibility Level to .NET framework in Unity (Edit > Project Settings > player > API compatibility Level), then the VS code will report an error, saying:
The type or namespace name 'Ports' does not exist in the namespace 'System.IO' (are you missing an assembly reference?) [Assembly-CSharp]. The weird thing is, Unity reports nothing. So I decide to ignore the error and press the play button anyway. But in runtime, Unity reports an error:IOException: No such file or directorySystem.IO.Ports.SerialPort.Open()(at sp.Open() in Start function)If I switch the API compatibility Level to .NET 2.1, then the Unity compilation will report the same error as above, but the VS code works just fine and causes the Unity project unrunnable⦠so confusing
Please help me with this so I can make the world better
Much Appreciated