0

I am using Unity 2021.3.29 and Visual Studio Code 1.81.1. I tried setting Visual Studio Code as the Unity C# editor by following the guide https://code.visualstudio.com/docs/other/unity. This is also what people on YouTube seems to be doing.

The problem I am facing is that when I first open a script through Unity it opens Visual Studio Code and it seems to recognize Unity code (as I am used of seeing it in e.g. Visual Studio 2019) but then 4-5 seconds later it seems to lose some connection. Please see before and after images below. I am getting no errors in console or anywhere, it just doesn't seem to recognize Unity things like InputField anymore and similar.

Last image is the Extensions I have active for the project if that helps.

How can I fix Visual Studio Code to work with Unity?

When it first fires up Visual Studio Code: enter image description here

After around 4-5 seconds: enter image description here

Extensions for Visual Studio Code:

enter image description here

eligolf
  • 1,682
  • 1
  • 6
  • 22
  • If I remember correctly I had to change the root folder of VSCode to the root folder of the Unity Project. So one folder above the Assets folder. – pixlhero Aug 21 '23 at 11:47
  • @pixlhero, I am not entirely sure what you mean. In Explorer in Visual Studio Code I do have the Project name at top, then Assets, then my scripts.I used the Open as C# project from Unity. – eligolf Aug 21 '23 at 12:05
  • then it's already setup. I think I also switched to omnisharp until the extension isn't in preview anymore. – pixlhero Aug 21 '23 at 12:16
  • for switching to omnisharp, see https://stackoverflow.com/q/76850403/11107541 – starball Aug 21 '23 at 17:00
  • Please copy and paste the output of the `Help: About` command (run in [the command palette](//code.visualstudio.com/docs/getstarted/userinterface#_command-palette)) into your question post. – starball Aug 22 '23 at 23:42

1 Answers1

1

There's a lot of discussion about IntelliSense-related issues recently in https://github.com/microsoft/vscode-dotnettools/issues/329. One user there has written a part-way TL;DR. I'll try my hand at it as well.

You're not the only one who observes this problem. See https://github.com/microsoft/vscode-dotnettools/issues/329#issuecomment-1668036903. This other user has what sounds like the same issue, and they found that if they configure a default solution for the VS Code workspace, then the issue is resolved. Another user also found this to be the solution for them: https://github.com/microsoft/vscode-dotnettools/issues/329#issuecomment-1666870206. Ex. in .vscode/settings.json:

"dotnet.defaultSolution": "path/to/your/solution/file/goes/here"

Another user in https://github.com/microsoft/vscode-dotnettools/issues/329#issuecomment-1668136038 found that they had to right click the project in the Solution Explorer and click "clean" there every time they re-open VS Code.

Another user solved their issue by a lengthier process involving updating a VS Code package and regenerating project files, and using pre-release versions of related VS Code extensions. You can find their writeup here.

There's also (at the time of this writing), a known bug where having the Unity Package Netcode for Entities crashes the Language Server Protocol providing code completion.

starball
  • 20,030
  • 7
  • 43
  • 238