# Use a different target framework version in a unity c# project, other than 4.6

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?

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

• 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. – Ed Marty Dec 23 '18 at 0:16
• 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. – Paiman Roointan Dec 23 '18 at 13:37
• 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. – Ed Marty Dec 24 '18 at 7:23
• 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. – Paiman Roointan Dec 24 '18 at 17:24

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()
{
Debug.Log("OnGeneratedCSProjectFiles");
var dir = Directory.GetCurrentDirectory();
var files = Directory.GetFiles(dir, "*.csproj");
foreach (var file in files)
ChangeTargetFrameworkInfProjectFiles(file);
}

static void ChangeTargetFrameworkInfProjectFiles(string file)
{