1
votes

I'm a new Unity user, and I'm having issues when trying to attach a C# script to a GameObject:

Can't add script component XYZ, because the script class cannot be found. Make sure there are no errors and that the file name and class name match.

Searching online this issue usually happens when the class name and file name are different, but I've checked that and they are the same. I even tried creating a new empty/default C# script and tried attaching that, but no luck. This is completely empty project, and there are no other scripts with compile errors. GIF showing the error

enter image description here

Any ideas, help would be appreciated?

I'm running Unity 2019.1.of2 Personal on Ubuntu 19.04

2
The resolution of that gif is giving me a headache.3Dave
Long shot, but try naming it something other than Test. (It shouldn't matter, but...) Also, are there any other errors or warnings in the console?3Dave
This is weird, but: in the inspector as soon as you create the script, there's a message "No MonoBehavior scripts in the file, or their names do not match the filename." Which jives with the error dialog. What happens when you open this up in your script editor? There could potentially be a permissions issue (or something) preventing the compiler from working correctly. I looked at the engine source, and this is generated when the script name can't be found in the assembly manifest. Can you confirm that the assembly is being generated?3Dave
If there are any compilation errors (even ones not related to the script you're trying to attach), the new script hasn't been compiled and therefor cannot be attached.Draco18s no longer trusts SE
I can't really see the gif but, i'll say this. Make sure you have no errors at all in the code, i'd recommend fully compiling it first using something like visual studio and then debug mode. If the code has any errors you will get that message because unity only accepts fully compiled and error free scripts to be applied to game objects.Corey Smith

2 Answers

0
votes

I managed to solve the issue with help from the comments above. The issue was that Unity was unable to compile/build the project, hence as 3Dave and Corey Smith said - if there are any compile errors you are unable to attache scripts to GameObjects.

I also realized I was unable to run the project. I first though it might be an issue with .NET versions on Ubuntu, but it turned out to be issue with libssl1.0 library (if you have a greater version then Unity won't work on Ubuntu 19.04). See this post for reference: https://forum.unity.com/threads/empty-log-errors-in-2019-1-0f2.669961/#post-4545013

Installing libssl1.0 solved the compile issues, I'm now able to attache scripts to GameObjects and run the project.

0
votes

Old question, but I solved this problem using the Help menu and then Reset Packages to defaults.