I'm using a dll in my project which is built against a higher target framework (4.6.1 actually)

I can't build or attach to Unity because of this. Visual Studio shows this error:

The primary reference "MQTTnet" could not be resolved because it was built against the ".NETFramework,Version=v4.6.1" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.6".

The only option in player settings in Unity for API compatibility level is .NET 4.6

how can I change that to 4.6.1 in order to be able to build or attach to Unity?


Add the following script anywhere in your project to automatically change the target framework of the c# project files.

using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public class VisualStudioProjectGenerationPostProcess : AssetPostprocessor
    private static void OnGeneratedCSProjectFiles()
            var dir = Directory.GetCurrentDirectory();
            var files = Directory.GetFiles(dir, "*.csproj");
            foreach (var file in files)

    static void ChangeTargetFrameworkInfProjectFiles(string file)
        var text = File.ReadAllText(file);
        var find = "TargetFrameworkVersion>v4.6</TargetFrameworkVersion";
        var replace = "TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion";

        if (text.IndexOf(find) != -1)
            text = Regex.Replace(text, find, replace);
            File.WriteAllText(file, text);

| improve this answer | |
  • \$\begingroup\$ an impressive tip \$\endgroup\$ – Fattie Aug 25 at 14:08

Found a workaround for this.

look for TargetFrameworkVersion in every .csproj file and change v4.6 to v4.6.1

I used a sed script to automate this, and run I it every time Unity generates a project:

sed -i 's/TargetFrameworkVersion>v4.6<\/TargetFrameworkVersion/TargetFrameworkVersion>v4.6.1<\/TargetFrameworkVersion/g' next-step/Source/*.csproj
| improve this answer | |
  • \$\begingroup\$ Just make sure this still works when actually compiling the game. I have a suspicion that it won't at least for some target platforms. \$\endgroup\$ – Ed Marty Dec 23 '18 at 0:16
  • \$\begingroup\$ My target platforms are Android and iOS and there hasn't been any problem so far. Also this will change .csproj files. Does Unity use Visual Studio .csproj files for anything when building? I guess it shouldn't and this can't cause any problem. \$\endgroup\$ – Paiman Roointan Dec 23 '18 at 13:37
  • \$\begingroup\$ It does not. What I'm getting at its that you are apparently using a third party library that requires .NET 4.6.1 but Unity uses 4.6. \$\endgroup\$ – Ed Marty Dec 24 '18 at 7:23
  • \$\begingroup\$ My guess is Unity is not actually using 4.6. 4.6 is just the general version number used in player settings. But it puts 4.6 as target framework in csproj files. We are extensively using that library in our project, and never had an issue with this. Unity must actually be using a higher and different version of .Net in builds. \$\endgroup\$ – Paiman Roointan Dec 24 '18 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.