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:
all of these Visual Studio refuses to build project due to missing assembly reference that isn't missing
all of these "Are you missing an assembly reference?" compile error - Visual Studio
Removing the .vs folder and all VS files from the Unity project folder and letting the editor regenerate them. Also regenerated manually from within unity
Making sure Visual Studio (and the Visual Studio Tools for Unity (which is installed using the Visual Studio installer)) are fully up to date.
Updated VS to the latest version
Removed VS Code package (I'm not using Code) from Unity, then readded.
A number of restarts of Unity and VS in between all the above.
EDIT: all of these http://www.codeandyou.com/2017/08/missing-assembly-reference-in-visual.html
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.
This issue has popped up again (I'm using 2020.2.1f1).
I fixed it by selecting Assets->Reimport All in the editor.
Turns out it was a bug in Unity. New update came out which fixed it. 8 hours of my life later...
I am using Unity 2020.3.18f1, with Visual Studio 2019.. Spend hours figuring out. I have tried:-
Deleting "*.csproj"
Unity > Edit > Preferences > External Tools > "Regenerate project files"
Unity > Edit > Preferences > External Tools > External script editor > Visual Studio
Rebuild Solution
Right click on .cs file in unity > Open C# project on the script
Reimport script
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.
Related
As stated in the headline, Visual Studio 2022 suddenly stopped showing errors (and also I can't find public method and variables but that's another question).
I am working in Unity and suddenly it just don't want to cooperate anymore.
I tried closing the application and unity all together and restarted my computer, and nothing worked.
At the top of your screenshot you can see "Miscellaneous Files". This means that the file is not considered to be part of any project.
To see errors and IntelliSense, VS requires source files to exist within a project so that it knows:
What references exist (packages, assemblies, target framework)
What version of the language is being used
Various properties that influence analysis/analyzers
So for some reason your file is not considered part of a project. We can't see your Solution Explorer, so it's not clear why that might be. Most likely you've opened the file via "File | Open". Make sure you create the file within a project, or add it to a project.
One of the endless joys of working with Visual Studio are random inexplicable times it stops working the way it should. Usually these steps work:
Close VS completely
Ensure all bin and obj directories of all projects are cleared
In the same directory as your solution should be a hidden .vs directory. Delete this.
Reopen VS and your solution. You should be back to a normal state within a few moments. Sometimes a "Rebuild All" can accelerate its return to normality too.
I'm in the process of updating a library in a larger solution. For this purpose, I have removed the old references in all projects and added new ones.
If I compile the solution now I get about 3000 build errors, which indicate that various classes are defined in the old, removed libraries and I should add a reference to them.
After I have opened a reported file, Visual Studio recognizes that these classes are also included in the new libraries and removes the error.
Now I don't want to open hundreds of files to make these ghost errors disappear, especially because the problem repeats itself after the next build.
Does anyone have any idea how I can get Visual Studio to stop reporting ghost bugs?
I'm using Visual Studio 2022.
When trying to reference a class from a Unity package (specifically InputSystem), VSCode will give me the errors The type or namespace name 'InputSystem' does not exist in the namespace 'UnityEngine' (are you missing an assembly reference?) [Assembly-CSharp], even though Unity builds and runs just fine with no complaints, and generating the project files for Visual Studio work properly.
This started when trying to move to VSCode from Visual Studio as my main editor, so something in that process likely broke them. I have tried a bunch of different things - deleting and regenerating the .csproj and .sln files, uninstalling, reinstalling, and updating the package, even deleting the whole repo and re-cloning but nothing has fixed the issue.
This is definetely a unity/ide integration problem. Try following this official guide to setup VSC Unity support.
Did you follow this tutorial instructions for using vs code with unity? Because some things need to be installed in order for you to use vs code with unity.
Since I do not have enough creds, I am writing this as an answer.
How did you move from Visual Studio to VS Code ? I hope you changed it in Edit > Preferences >External Tools> External Script Editor.
Have you tried to regenerate the Project Files from Unity?
Edit > Preferences > External Tools > Regenerate Project Files
This usually works fixing corrupted (or not compatible) *.csproj files.
Starting up a new Unity game project, and using Visual Studio 2019 Community (VS) for my C# development. Unity shows errors in the console window but I find that limiting and inconvenient. I would like to enable all errors, warnings, and suggestions in Visual Studio itself.
I have "Disable the full build of projects" set to false in VS (in the "Tools for Unity" Options tab). I have my error list dropdown set to "Build + Intellisense". Changing any of those flags doesn't seem to do much of anything. I am using Visual Studio 2019 and Unity 2020.2.4f1. I don't see a csproj file or any other project files in VS or Unity or my Windows Explorer. It appears to build because I can play in the Unity editor.
Is it possible to show the errors in VS's Error List window with Unity projects or not? How do I set it up to show errors in VS for Unity projects?
EDIT: One more thing, I am unable to see Intellisense suggestions at all for my project as I write code in VS so it makes coding that much slower and inaccurate.
Found the answer on a different site than StackOverflow and verified it this morning.
In Unity, go to Edit->Preferences->External Tools.
Change the External Script Editor to your instance of Visual Studio.
In the new options below for Generate .csproj files for:, make sure Embedded packages and Local Packages are selected.
Click the button Regenerate project files below and close the Preferences window.
Close Visual Studio.
Double click on any of you scripts in Unity to open up Visual Studio again with your scripts.
You should see your script in Visual Studio opened up with the .csproj files setup for your Unity Project's Solution in the Solution Explorer panel (if you have it open), with the whole project open in Project mode. Go through your Assembly-CSharp solution to see your code (typically within Assets).
Open the Error List panel, and in the drop down, select Build + IntelliSense.
Purposefully make a mistake to see IntelliSense in action producing errors, warnings, and suggestions in the Error List below.
Bonus Step: If you want to granularize the suggestions in the Error List, change your Code Style settings in Visual Studio in Tools->Options->Text Editor->C#->Code Style to change how you wish Visual Studio to prefer to act with your code style. You can look through the C#, C#_LSP, and All Languages to make changes to tabbing, formatting, and so on. Up to you. Remember that Unity 20.2.4f1 supports up to C# 7.3 using the Roslyn compiler (so make code style changes and suggestions as appropriately).
We had this issue and after 4 days testing all the possible solutions we found on the internet we tracked down the problem being that the project's file path had the character "%20", after renaming the folder to a name with only letters and numbers both Visual Studio Community and Rider started working properly.
All of a sudden when I try to attach to Unity 5.2 with Visual Studio 2015 I get a bunch of errors as seen in screenshot below. This is the second this has happened this week. Last time I had to create a new project and bring everything over to resolve the issue. Unity plays fine, no errors...but visual studio throws errors when I try to attach. It worked earlier this evening.
Few things to consider when project shows error while being fully functional in Unity3D Editor itself:
Can you build your project in Visual Studio even if errors are shown? If so, there is possiblity that these are just leftovers from Unity3D building your project - should disappear on first successful build from VS
If you can't build your project, have you tried to remove your Library folder from project's root folder? In many cases it will fix the problem but will end up in forcing Unity3D to rebuild some of the files it had previously in Library folder.
Have you added any new libraries (dll's) to project? Any chance they might be installed/added in a wrong way? If so, try readding them to project (if it's a classic dll) or readd plugin that you have troubles with.
I would leave version incompability as it would end up with errors in Unity3D console. Same with errors with solution per se.
When I changed target framework to Unity 3.5 .net full Base Class Libraries the build errors went away.
I found the solution posted by Nathan Koop here: Type or namespace name does not exist