VSCode can't load C# project!

I open scripts by vscode, but get error, and There are no code hints!
Error info:

2023-07-27 20:12:35.538 [warning] The project file '/home/Workspace/U3DTrain/Assembly-CSharp.csproj' is in unsupported format (for example, a traditional .Net Framework project). It need be converted to new SDK style to work in C# Dev Kit.
2023-07-27 20:12:35.698 [error] Failed to load project '/home/Workspace/U3DTrain/Assembly-CSharp.csproj'. One or more errors occurred. (This project is not supported in C# Dev Kit.)
2023-07-27 20:12:35.709 [info] Project system initialization finished. 0 project(s) are loaded, and 1 failed to load.

Environment:

OS: Deepin 23(Debian 10.10)
Editor: VS-code(with plugins .NET Install Tool for Extension Authors, C#, C# Dev Kit, IntelliCode for c#, Debugger for Unity)
apt Installed: dotnet-sdk-7.0, mono-devel, mono-complete, mono-dbg

Version:

dotnet: 7.0
mono: 6.12.0.200
vscode: 1.80.1
UnityEditor: 2022.3.5f1 LTS

6 Answers

6

If you are getting this issue after you got the new update to the vs code and before everything was working normally
To solve the issue

  1. Download and install the official version of the unity extension . Announcing the Unity extension for Visual Studio Code - Visual Studio Blog
  2. Go to package manager and unity registry , search “studio” and update both extensions to recommended version.
  3. Open the VSCode and check for code completion
  4. If still no code completion , Open the project folder and there you can see all your csproj files for eg : “Assembly-CSharp.csproj”
  5. Open the csproj file and search for word “TargetFramework”. You will see one like this <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
  6. <TargetFramework>net7.0</TargetFramework> copy this and paste under the step 5 line. Do same for all your csproj files if you have more than one.
    Now you can see code completion and syntax highliting etc.

I have tried everything to fix the code completion. Nothing worked except this. Thank you so much!

This actually works.. BUT !!! As soon as a new script is created or similar, Unity overwrites the .csproj file, and we are back to it not working.. Can we get this fixed in unity .. PLEASE :) Right now useless :( I can only suggest people to NOT upgrade unity and/or visual studio code! The good old PackageCache/com.unity.ide.vscode@1.2.x/Editor/ProjectGeneration/ProjectGeneration.cs sadly also does not work anymore.. It will overwrite the file during startup.. UPDATE: however - updating/hacking the com.unity.ide.vscode-1.2.X.tgz will make it work.. Hurray.. But what a haxs

@jesniels: Where can one find the com.unity.ide.vscode-1.2.X.tgz for hacking?

@fseydel Depends on your installation.. Just search of it :P On my default vmware installation I dumped it in Program Files on Windos.. So here the path is; C:\Program Files\Unity\Hub\Editor\2022.3.6f1\Editor\Data\Resources\PackageManager\Editor ... Depending on your setup - you might also "simply" change the file Projectgeneration.cs here: AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.ide.vscode@1.2.5\Editor\ProjectGeneration .... I changed them both to be sure... On existing projects: <project>\Library\PackageCache\com.unity.ide.vscode@1.2.5\Editor\ProjectGeneration :)

Using this way I got rid of this error, but I got another one: The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks I'm working on Ubuntu 22.04 (everything up to date)

I found another, less hacky, solution.

First, make sure you are using Unity 2021 or above

In VSCode:

In Unity

  • go to Window → Package Manager
  • remove the “Visual Studio Code Editor” package (it’s deprecated). If you’re unable to uninstall this might be because it’s part of the “Engineering feature set”. You can remove this package and add it’s contents manualy (you can see what’s part of this set here).
  • Install and/or update “Visual Studio Editor” (version 2.0.20+). From this version and higher it also includes Visual Studio Code support.

In File Explorer

  • Delete all .csproj and .sln files from your unity project folder (Make a back-up if you want)

In Unity

  • Go to Edit → Preferences → External Tools
  • Set VS Code as Code Editor
  • Click “Regenerate Project Files”
  • double click any script to open VSCode

Thank you shamsad for pointing me in the right direction. Also thanks to this issue page on Github (github .com/microsoft/vscode-dotnettools/issues/271#issuecomment-1672096480)

Sorry for the misformatted links. As a new user I’m not allowed to post more links.

It works! thanks

It seems that Unity updated the package manager to mark 'Visual Studio Code' as deprecated, exclude it from the 'Engineering' feature set and update 'Visual Studio Editor' to version 2.0.20. This should make everything easier for everybody as you wouldn't have to delete the feature set and can just remove 'visual studio code editor' and update 'Visual studio editor'.

That helped. Thanks!

It worked. Thank you!

it worked finnaly! Tks so much!

Solution:
In a word:
Install “Unity” extension in Visual Studio Code and Update the “Visual Studio Editor” package to at least 2.0.20 in Unity Package Manager.

Details:

  1. In Visual Studio Code, search “Unity” in Extension marketplace and install it. In the details of “Unity” extension, you also can find the requirements Unity settings.

  2. In Unity, WindowPackage Manager, find “Visual Studio Editor”, Unlock it and Update it to the latest version (2.0.20+).


  3. In Unity, SettingsExternal Tools, select “Visual Studio Code”, make sure it shows “Visual Studio Editor v2.0.20 enabled” (the version same as you installed in the previous step), then Regenerage project files.

  4. Then back to Visual Studio Code, just click F5 to start your happy debugging.

That's help thank you!

In VSCode go to settings and look for omnisharp.useModernNet
uncheck and restart VS Code
Load unity solution from unity
Autocomplete should work.
make sure you have dotnet SDK and MS build tools installed.

Hope this helps,
Rahul

Tried this out.. Without the <TargetFramework>net7.0</TargetFramework> in the .csproj it still does not work for me.. It was interesting that after disabling that, it downloaded a "dotnet.exe" of the same 7.0 version, he he

For me it worked. I had to install Microsoft build tools too.

I recently found that dotnet.server.useOmnisharp this need to be true in settings.

It seems like both files Assembly-CSharp-Editor.csproj and Assembly-CSharp.csproj are missing the line:
<TargetFramework>net7.0</TargetFramework>
After adding it everything works fine.

The question is, how to make this permanent? Because every new project will be generated, it misses the TargetFramework on both files, any fix for Unity on that?

Thank you so much. I’ve spent all night looking for a solution and nothing worked. Nothing, except this. Thank you again