27
votes

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

8
unity5 to 2018.2.2f1 is a big jump. It would have been better to do unity5 to 2017 then 2018.2.2f1 to reduce the chances of issues arising in the updated project. Where is the "CubeScript" placed in your project?Programmer
@Programmer It is in my assets in a folder _scripts and I opened it from the assets and the script opens as usual in monodevelop.Youssof H.
There is a floder Packages next to Assets do I have to do something with it? @ProgrammerYoussof H.
That is totally not professional I want another solutionYoussof H.
My Project Is so big and i can't go to every folder and do that manually is there any way better than thisYoussof H.

8 Answers

38
votes

If you still have the old copy of the project, upgrade the Unity project to Unity 2017 first then to 2018.2.2f1.

Here are the few possible reasons you may get this error(Ordered from very likely)

1.Script name does not match class name.

If script name is called MyClass, the class name must be MyClass. This is also case-sensitive. Double check to make sure that this is not the issue. To make sure that's not the issue, copy the class name and paste it as the script name to make sure that this is not the issue.

Note that if you have have multiple classes in one script, the class name that should match with the script name is the class that derives from MonoBehaviour.


2.There is an error in your script. Since this is an upgrade, there is a chance you're using an API that is now deprecated and removed. Open your script with Visual Studio and see if there is an error there then fix it. There is usually a red line under a code that indicates there is an error.


3.Bad import with the Unity importer and its automatic upgrade script.

Things to try:

A.The first thing to do is restart the Unity Editor.

B.Right click on the Project Tab then click "Reimport All"

C.If there is still issue, the only left is deleting the problematic script and creating a new one. There is an easier way to do this if the script is attached to many GameObjects in your scene.

A.Open the script, copy its content into notepad.

B.From the Editor and on the Project tab right click on the script "CubeScript", select "Find References In Scene".

C.Unity will now only show all the GameObjects that has this script attached to them. Delete the old script. Create a new one then copy the content from the notepad to this new script. Now, you can just drag the new script to all the filtered GameObject in the scene. Do this for every script effected. This is a manual work but should fix your issues when completed.

7
votes

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.

0
votes

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.
0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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.

0
votes

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.