I have been using Visual Studios for a couple of weeks now, however, I always still created new (C#) scripts using the option in the Unity project manager. For a change, I decided to try the ‘add class’ button inside of Visual Studios, which led to a freeze of Visual Studios. So I started everything up again and found out that several things have gone terribly wrong.
First, whenever I create a new script, it is created as belonging to a ‘project’ called ‘miscellaneous files’ instead of the designated project files. (This is inside of Visual Studios. Inside of Unity, the script is placed inside the right folder).
Second, whenever I now try to attach a script to a gameobject, I get the error: “Can’t add component X because it doens’t exist. Check to see if the file name and class name match.” Ofcourse these names matches, because I have been using the scripts succesfully over the last few weeks.
I’ve re-installed Unity, and it the problem is still there. When I create a new project or open a different project, the problem does not occur. I’ve searched the Internet trying to find a solution, however, the answers there have not yielded any result. Anyone here who might know the answer?
Thank you both for your answer! I tried both your suggestions, but both didn’t help… However, I started Unity this morning, and ‘it’ acted like nothing had happened yesterday. So I have no clue what went wrong, but I’m glad to be able to continue.
You may have renamed the script file or class name.
Reload your solution in visual studio / IDE.
If that does not work, delete the .meta files for those scripts and unity will reimport.
Reimport all if still a problem, and you can also delete your library folder and let unity regenerate it if its struggling to link the meta to the script.
Not really a simple fix but it works, if you a .csproj file that has .Plugins in it, eg. SampleProject.Plugins.csproj. Open it up in a text editor, preferably notepad++. Go to the bottom of the file and you should see something like:
My understanding of the build process is extremely limited, so please take this with a grain of salt:
As I understand it, Mono and Visual Studio both compile their own assemblies which each represent their own versions of the source files.
With this in mind, it sounds like you have a mismatch between the Visual Studio and Mono assemblies: Visual Studio solution contains the class, but Mono assembly does not.
Typically when you get these weird desync issues in Visual Studio (I often get Resharper deleting all my “using” directives) you can solve them by doing a manual synchronisation of the MonoDevelop project: