Search code examples
c#visual-studiounity-game-enginevisual-studio-2017.net-assembly

Why do my namespace references keep breaking in Visual Studio?


I asked this question on the Unity Forums but unfortunately no response there. Since this seems to be an error/issue with Visual Studio itself I am asking here as well.

Situation

I am unable to compile my project code in Visual Studio. It still compiles and runs in the Unity Editor but Visual Studio gives hundreds of missing reference errors ("The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)"). This includes files which I wrote but which are in another assembly, i.e. my own created namespaces.

Nothing I've tried works (see below) except for one thing. If I manually remove the references to all four assemblies in visual studio and re-add them, the solution will compile. But when I then close VS and reopen the same error comes up. From more testing I found that now and then (don't know what triggers it) the references will even break without closing Visual Studio.

The assemblies are Assembly-CSharp, Assembly-CSharp-Editor, Assembly-CSharp-Editor-firstpass and Assembly-CSharp-firstpass; the normal ones that get created with a Unity project. The scripts which now do not reference correctly in the assembly did not cause any problems before and I did not change them at all. I do not know what I would have done that is now causing this to break. Previously there were no errors coming up with these scripts; one moment they were compiling fine and the next these errors were coming up.


Versions

Unity 2019.3.12f1 Personal.

Visual Studio Community 2017 15.9.22.


I've tried:

Edit:

I've deleted large parts of the project in a copy to see when it would solve. It does not solve all the way down to removing the last script. Then removing that last script and adding a different one again results in the error. I don't see this is anything wrong with the scripts themselves but must be something with VS settings or behavior.


Solution

  • Turns out it was a bug in Unity. New update came out which fixed it. 8 hours of my life later...