When I try to drag a script onto a game object–in this case “Test_script”–I get the following error message in a pop-up window:
Something mysterious recently happened to my project, and now I get this message every time I try to add scripts to game objects, despite their class names matching their filenames (I’m using C#). I fear some “metadata” has been tweaked in my project.
Any suggestions? Thanks.
You probably have a compile error in one of your scripts. Try triggering a recompile and take a look at the console window to see what errors are reported.
Are your class names matching the file names?
e.g. the script called MyClass.cs must contain a class called MyClass that derives from MonoBehvaiour
Thanks for your suggestions. I forced a compile error to verify console reporting, so I don’t think I have any compile errors. I also double checked my class names against the script names.
I should point out that we have several levels completed and about 8000 lines of script written for our current game, and I haven’t had this particular problem before.
I learned long ago not to move files around in Finder that are located under the Assets folder, because it corrupts some file location metadata that Unity keeps around. I’m wondering if some other type of metadata corruption has occurred somehow. Is there any way of “rebuilding” what Unity thinks the compilation state is for the scripts?
You can use Assets → Rebuild. If the problem persists, send in the project folder via Report Bug.app
I don’t mean to resurrect this old thread, but it’s the top google result so I thought I’d report my findings.
I had the same problem that wasn’t solved by any of the above steps. The issue for me ended up being that the script I was trying to add as a component was inside a namespace, which we all know Unity does not like. Putting the script class back into the global namespace (sigh) fixed the issue for me.
Thanks seandanger , Your tip helped me.
Also, if you don’t want to put that script in the global namespace (you may want to import it for use in a more complex class) you can just make another script in the global namespace that inherits from that script. As long as you make all of the Unity methods “protected”, your new script will do everything the old script did, even though it appears empty.
For me, the issue was that the script I was trying to attach was inside a folder named “Editor”! I had done this purely for organization but forgot that Unity treats the “Editor” folder as something special. Editor scripts can’t be attached to GameObjects, and they won’t be included in builds.
A good debugging solution that led me to this cause was simply trying to drag and drop the script onto an empty game object. Unity gave me a helpful error message.