Unity pre-preocessor defines not recognized in VS 2022 for Windows

I have a new installation of VS 2022, Unity 2021.3.5 on a new Windows 11 laptop.

I managed to get everything running so that VS recognizes Unity and can attach the debugger to it.

However, the built-in Unity pre-processor defines such as UNITY_EDITOR are not being recognized by VS. For example, if I put some code in between #if UNITY_EDITOR and #endif, that section of code is grayed out in VS, but Unity is still recognizing it and compiling it in the editor.

Is there a VS setting I’m missing?

Hm, I don’t know if I’ve seen that. I can think of two things to try:

  1. In the Editor go to Edit → Preferences → External Tools. Check that Visual Studio 2022 is selected. Click the button that says Regenerate project files.

  2. Look for the Visual Studio Installer in the Windows search bar. Click the modify button in the 2022 installation. In the Workloads tab, scroll to the Gaming section. Make sure that Game development with Unity is selected. If it isn’t, close everything else, select it, and press the Modify button.

1 Like

What oscar says above, plus some more:

This may help you with intellisense and possibly other Visual Studio integration problems:

Sometimes the fix is as simple as doing Assets → Open C# Project from Unity. Other times it requires more.

Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

Barring all that, move on to other ideas:

Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button

Also, this: No suggestions in Vscode

Thanks for all the suggestions. Like I said, it’s a brand new installation, so I doubt it’s from old data getting bad. I already made sure I have the Unity workload selected and got intellisense and attaching debugger working. This seems to be the only thing. I’ll try a few of the other ideas above soon.