5

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.

D3Duck
  • 77
  • 1
  • 6
  • 2
    _Were all working before_ what? Create a brand new one from scratch and see if that works. If the new scratch one doesn't work, you have an installation problem. If the new one works, start copying important parts of yours to the new one and see where it breaks. Just break it down and be methodical. This is a debugging exercise. – Wyck May 11 '20 at 12:59
  • @Wyck I've edited the question to be a bit more clear. A few days ago I was writing some code and it was compiling, an hour later it wasn't and I was not even working on the scripts that are now not referencing. I don't think copying scripts is going to resolve this as it will cause way more compiler errors (actual ones) and this problem is related to the assembly references in Visual Studio. As I mentioned, in the game engine it is compiling and running fine and when I manually fix the references in Visual Studio it will compile just fine, to then break again later for no obvious reason. – D3Duck May 11 '20 at 13:29
  • Bump and edited – D3Duck May 13 '20 at 04:03

4 Answers4

3

This issue has popped up again (I'm using 2020.2.1f1).

I fixed it by selecting Assets->Reimport All in the editor.

2

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

D3Duck
  • 77
  • 1
  • 6
  • 1
    I think I'm facing this issue now, but it should be a version later.. Can you specify which versions are involved? I'm on 2019.3.12 – Joseph Jun 05 '20 at 05:44
  • I am on 2019.3.2f1 and having a similar issue. Please let us know which version update fixed the issue. The compile works just fine from within the Unity editor, but the generated Visual Studio projects contain references with warning icons over them. – vargonian Nov 24 '20 at 09:20
2

I am using Unity 2020.3.18f1, with Visual Studio 2019.. Spend hours figuring out. I have tried:-

  1. Deleting "*.csproj"
  2. Unity > Edit > Preferences > External Tools > "Regenerate project files"
  3. Unity > Edit > Preferences > External Tools > External script editor > Visual Studio
  4. Rebuild Solution
  5. Right click on .cs file in unity > Open C# project on the script
  6. Reimport script
  7. Create new script and open, to trigger recompilation

None of these is working for my case. Problem gone after I reload the assembly in "Solution Explorer" of Visual Studio. Do to the rest of "unloaded" assembly.

enter image description here

Tengku Fathullah
  • 1,279
  • 1
  • 18
  • 43
0

first, check if the auto referenced of your assembly is checked

auto referenced

second, for visual studio, maybe we should do one more step:

Unity > Edit > Preferences > External Tools > "Regenerate project files"

Voidsay
  • 1,462
  • 2
  • 3
  • 15
Shell Lee
  • 1
  • 1