Visual Studio throws errors when attaching to Unity - c#

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

Related

Unity/C# - Compiler Errors ONLY in VSCode, not Unity Editor or Visual Studio

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.

Unable to run a MonoGame project on a clean installation of Visual Studio and MonoGame

I recently got a new computer, and finally got around to installing MonoGame, as I worked with it a few months back on my old computer, and primarily use Unity for my game development when not working on 2D games. Their website says they have CLI templates that work with Rider, but I figured it would be best to get it installed with Visual Studio first. Following the exact steps listed on the MonoGame website, I got everything installed that I needed, and started a project from one of the Visual Studio MonoGame templates. Upon opening the project, and everything finishing loading, I am greeted to this miserable sight. I also had an error in the console relating to MonoGame, with the error code MSB3073. From what research I did, I found that it either had to do with my username having a space in it (it clearly does not, so that was out of the picture), or it had to do with using the Visual Studio templates, and I would simply need to create the project from the CLI templates, as seen here. However, creating a new project from the CLI templates does not change anything. I have tried uninstalling and reinstalling dotnet, I have tried uninstalling and reinstalling Visual Studio. I have, of course, powered my computer off and back on multiple times. Even trying to open the project in Rider after installing the proper MonoGame packages in the program has the same exact errors. But when I zipped the project up and sent it to a friend who works with MonoGame, he was able to run the project with no issues, so clearly it has something to do with my setup.
I have also cleaned my nuget cache, cleared my project temp folders, and cleared the visual studio cache. Eventually, the Microsoft dependencies stop having issues when I simply close and open the project again, but every time I open the project in Visual Studio, I get the same error in the console, and it prevents me from running/building the project.
Hoping someone on here will know what to do with the info I've given, as I'm completely out of ideas!
So I figured it out. I'm not very experienced with the command line, but I managed to find a very nice tutorial by someone who wanted to make games with MonoGame using only the command line. In it, I was able to attempt to build the project from the CLI, and it gave me an error message unlike what was in Visual Studio. It said that MonoGame only works on a 64bit OS, but I'm on a 64bit OS. So I went to look at what version of dotnet was being used, and sure enough, it was the 32 bit version! It took a while to figure out how to uninstall it, and how to use the dotnet-core-uninstaller tool in the command line. Then all I had to do was remove the Environment Variable in the System category for the Path variable that referenced the Program Files (x86) directory of dotnet.
Hopefully if anybody else comes across this issue, they'll find this post, as none of the others seemed to fully explain how to do anything that needed to be done to fix the problem :)

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:
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.

Unity: Omnisharp is not finding referenced projects in VSCode

I recently upgraded to Unity 2019.3.0f6. When I opened one of my c# files, I noticed that UnityEngine.UI wasn't being recognized as a valid namespace. This caused me to get red squiggles whenever I referenced this namespace. IntelliSense also stopped working. This problem only shows up in VSCode, my project still runs just fine in Unity.
I checked the Omnisharp logs and found some errors that tell me Omnisharp is unable to find the UI .csproj file and a couple others.
[warn]: OmniSharp.MSBuild.ProjectLoader
The referenced project 'UnityEngine.UI.csproj' does not exist.
What would cause this?
Things I've already tried:
Checking that the dotnet command works
Re-installing the c# vscode plugin
Deleting the .sln and .csproj files and letting Unity regenerate them
Re-installing Unity
It is a bug in version 1.1.4 of Visual Studio Code Editor package.
These are the steps that I did to fix this issue:
Open Window > Package Manager in Unity Editor
Select Visual Studio Code Editor and click Remove
Select "See all versions" under Visual Studio Code Editor
Select version 1.1.3 and click Install
Restart VS Code
There is a forum discussion on this here
And the actual issue is being worked on Unity Issue Tracker ticket here
UPDATE:
The current Visual Studio Code Editor package version 1.2.1 does not have this bug.
There is Assembly-CSharp.csproj file at the root of Unity project. I added this to fix error:
<Reference Include="UnityEngine.UI">
<HintPath>Library/ScriptAssemblies/UnityEngine.UI.dll</HintPath>
</Reference>
In newer Versions of Unity this might still happen to you, and you may try these answers, but first make sure you have activated "Embedded Packages" and "Local Packages" at least in the Project settings if using VSCode and regenerate them; at least for me it made the errors due to missing Assemblies go away. Maybe for other issues you can try ticking others as well.
Of course my assemblies are set up correctly in Unity to reference each other. Just wanted to add this for those who maybe overlook that setting in search for a solution (I am pretty sure the original author already did this, but I see a few comments that hint me, that not everyone tried that first)
Enabled Sections in the Setup:
I'm having the exact same problem today. Didn't change anything. It just broke. I think the C# plugin in VSCode needs updated but I can't be sure. I'm fairly new to Unity but that seems to be the case. In the mean time I switched to VS2019 and it seems to be working. But I much prefer VSCode so I am looking for a solution too.
VSCode on a Mac. Problems with Omnisharp not loading.
YouTube user Axiom. He explains how to fix Omnisharp loading. After I followed his video, It fixed my loading problem.
The Fix.
On your C# exstions under the gear pick "Extension Settings" then in the search bar add "useGlobalMono" - Under Use Global Mono - change "auto" to "always" and restart Omnisharp.
Video Reference - https://youtu.be/KJYrRv9cShY

Warning on dependencies when create a new standard .NET project

I have installed VS 2017 and I am sure all features related to.Net Framework, Core, C#, and ASP.NET are installed with others.
All I do is create a new project (.net standard class library) then the dependencies section is showing the warning (yellow triangle) sign. When I click on the dependencies visual studio freezes and close. If I try to work with any library I get the same error
cannot resolve symbol
Any ideas please?
When I try to build the solution, I get the following error
EDIT
I have also checked NuGet Package manager and found that the available source looks like below
Well, What I am going to say will sound CRAZY, but it is a fact and known issue and really hard to be found.
The problem is in the project path, the path for the project I have created contained a special character c:/users/Hussein Khalil(XXX)/PROJECT_NAME
when I created the project on a simple path c:/test/... everything is worked fine.
Hope this could help.
I faced the same issue and it was not related with special characters in project path.
I executed "clean solution" and then "build solution" and it restored the dependency successfully. It solved the issue.

Categories

Resources