'Can't add script component because the script class cannot be found?

Yesterday I updated unity from unity5 to 2018.2.2f1. Unity scripts are not loading after Update 2018.2.2f1.

Once I try to play the Scene the scripts are not loaded and I can't add the script again it gives this error:

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

Error



Solution 1:[1]

Posting here for historic purposes and if someone from Google ends up here, I strictly had the same problem (I'm on Windows 10) and here's how I fixed it :

For me, the problem was that "Tools\Roslyn\csc" could not be found, something that Unity uses to compile C# files.

  • I opened C:\Program Files\Unity\Hub\Editor\2019.2.14f1\Editor\Data\Tools\RoslynScripts\unity_csc.bat and I added ".exe" to the line "%APPLICATION_CONTENTS%\Tools\Roslyn\csc" /shared %*

(This line should now be : "%APPLICATION_CONTENTS%\Tools\Roslyn\csc.exe" /shared %*)

Now it works perfectly.

Solution 2:[2]

Here, is some reason why that's heaping.

1. Your Script Name And your class name isn't same.

2. May be there is an error in your script.

Here, is the few way to resolve it.

1. Make a same name of script and class in to this script.

2. find there is the error? and try to resolve it.

Solution 3:[3]

I'm completely new to Unity and game development, but I found that making the error true by entering an incorrect name, then entering the right one seems to "trick" Unity into fixing its error.

Solution 4:[4]

I had the same error show up but for different reasons (in case someone ends up here from google).

I was a beginner and named my file with a space in it. I got the same error after that. Make sure to NOT include spaces in your C# script.

Solution 5:[5]

One more thing which might help apart from other answers

  • Make the class which inherits from the MonoBehaviour the first class in the file. It fixed this issue for me.

Solution 6:[6]

Checking the console bugs and fix them is a good way to solve the problems "Can't add script component because the script class cannot be found?", although the bug seems to be irrelevant. This works for me when I fix a bug in the other script.

Solution 7:[7]

for me I found out it was a script that I wasn't using which was linked to the player and other scripts that I deleted.

Solution 8:[8]

For me, the problem was that there was an error in another script.

Once I fixed the error, I could FINALLY attach my other scripts onto my gameobjects.

Solution 9:[9]

I just had this issue where I had a perfect script, no errors, and the same name as it should be but no matter what it wouldn't let me add it to a gameObject. There was another script that was completely unrelated but it had an error in that and somehow that prevented me from adding my good new script.

Solution 10:[10]

Like some folks already posted here - the issue might be of a different code. I was trying to add a script that was referenced by another one which had errors - it doesn't tell you that is the reason, although visual studio kinda suggests it by showing you the errors in the open script.

Just delete the bigger (errored) script and start adding to the unity project from the basic ones.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NhgrtPlayer
Solution 2
Solution 3 Dharman
Solution 4 BoyAcc
Solution 5 Jeevan MB
Solution 6 Jack Jane
Solution 7 player1
Solution 8 TheProgrammer
Solution 9 Trav
Solution 10 Boniek Jr