- Home /
System.IO.Ports missing for Unity with .NET 4.x
I very recently downloaded the stable release for Unity (2018.3.5f1). I have a project that talks to a microcontroller over the serial port, and in the past have used System.IO.Ports with no issue. However, just trying to import the namespace System.IO.Ports gives me this error in the editor and I am unable to compile:
error CS0234: The type or namespace name 'Ports' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)
As you can see below, my configuration settings are to use .NET 4.x and compatibility level is set to 2.0 (NOT subset, which isn't even available in the list):
If I set the "Scripting Runtime Version" to ".NET 3.5 Equivalent", the problem goes away. The only problem is that 3.5 is deprecated, and I would like to use something a little more future proof. Am I doing something wrong here? Do I need to check my .NET 4.x installation to see if it is missing some libraries? According to MSDN documentation, System.IO.Ports should be available in .NET 4.x. This problem persists across multiple machines & Unity installations of 2018.3.x. Any help greatly appreciated!!
Answer by xxmariofer · Feb 20, 2019 at 03:58 PM
Hello, you need to set api compatibility level to net 4.x(tested with 2018.3.0f2 version)
THAN$$anonymous$$ YOU! I could have sworn I tried that but indeed, switching the compatibility level to 4.x fixes it! After lots of searching and many posts from older versions that weren't relevant for me, I finally have the solution. Thanks again!
Thanks a lot, worked the same with my 2019.4.16 version
Answer by soccer_guru · Mar 14, 2019 at 06:29 PM
I had the same problem, and I can confirm that you first need to switch to the deprecated .NET 3.5 Equivalent first, before switching to .NET 4.X and setting the API compatibility level. Switching the API Compatibility level first, weirdly, doesn't work.
i didnt understand your post, you cant set up the compatibility level before changing the scripting runtime version since the first one is dependant of the second one. you cant have .net 4x without 4.x runtime version
Hey @xxmariofer, I meant to say that switching to .net 4.x equivalent and setting the API compatibility level .net 4.x didn't work at once for me. It only worked after I switched to the .net 3.5 and then switched back to .net 4.x.
Answer by CVTunity · Apr 30, 2019 at 03:16 PM
I also had the same problem. I used scripting runtime version .net 4.x Equivalent and Api Compatibility Level .Net 4.x. But I needed to build the solution in the IDE after switching from .Net Standard 2.0 to .Net 4.x