How to force Unity to compile same way as VS?

Hi,
I’ve got some issue with my C# code in Unity project. Code works great in VS project, but doesn’t in Unity. Here what I found:

Part of 100% perfect-working code from regular Visual Studio C# project.
“Dictionary” reference to System.Collections.Generic namespace from System.Collections dll with 638 lines of code…

And the same code, in Unity C# script, with error:
CS1503 Argument „1”: cannot convert from
„System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<T, System.Collections.Generic.HashSet>>”
to
„System.Collections.Generic.IDictionary<T, System.Collections.Generic.HashSet>”
“Dictionary” reference to System.Collections.Generic namespace from netstandard dll with 81 lines of code…

Is it possible to change somehow Unity settings, to force it to use same dll’s as VS and run my code properly?

What target framework is your project in Visual Studio targeting? That constructor isn’t part of .NET Standard 2.0 I believe (and Unity targets .NET Standard 2.0 when building your code by default).

.NET 4.x, I changed it before.

.NET 4.x is a Unity option - I meant what are you targeting in Visual Studio?

.NET Core 3.1 / .NET 5.0 - both works good.

Yeah so Unity doesn’t support those targets. To match Unity, you either need to compile for .NET Framework 4.7.1, or for .NET Standard in Visual Studio (depending on whether you target .NET 4.x or .NET Standard 2.0 in your Unity settings).

1 Like

Thank You,
So… it means Unity won’t support my code whatever I change in settings, unless I change the code, to works good in VS in:
a) WindowsFormsApp (.NET Framework 4.7.1 & Unity settings on .NET 4.x), or
b) C# ClassLibrary (.NET Standard & Unity settings on .NET Standard 2.0),
right?

Correct. Class Libraries can also target .NET framework, by the way.

1 Like