53

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

6
  • 2
    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? Commented Aug 6, 2018 at 18:24
  • @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. Commented Aug 6, 2018 at 18:26
  • There is a floder Packages next to Assets do I have to do something with it? @Programmer Commented Aug 6, 2018 at 18:30
  • That is totally not professional I want another solution Commented Aug 6, 2018 at 18:41
  • My Project Is so big and i can't go to every folder and do that manually is there any way better than this Commented Aug 6, 2018 at 19:01

17 Answers 17

78

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I appreciate your answer but public datatypes used in FPSController all is gone and other scripts also have reset the data I entered in unity Editor's inspector window.
If #3 is your last resort then you have to manually do it. Unless you have the old project, there is really nothing you can do other than recreating and re-assigning them yourself. I know it sucks but I always tell people to do variable initialization via script and this is why. Sometimes, scene can go crazy due to an update or crash. If the initialization such as variables and script attaching is done via script then all you have to do is drag the script to another folder then drag it back.
It happens. You don't even have to update Unity for that to happen. I think you should take my advice and start attaching scripts from scripts. This will save you time in from having the-same issue in the future. Always make a copy of the project before updating it. This is a must.
@Confused Yes. Instead of dragging and dropping the script on a GameObject which is easier. you should create a script named ComponentInit that is attached to an empty project. That script you should use to find GameObjects then attach the necessary scripts to them. This is true especially when you have large project. For example, GameObject shipObj = GameObject.Find("Ship").AddComponent<ShipScript>();.
Oh Thanks that is important it is the first time I see .AddComponent That would be much handy.
|
10

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.

Comments

7

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.

2 Comments

Is this dangerous?
It shouldn't be, it only redirects Unity towards the correct executable to use, nothing to worry about.
1

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.

1 Comment

Everything was correct, and I was still with error, this answer worked for me
1

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.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

0

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.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

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.

Comments

0

I had this issue where I had a good script, no errors, good name, restarting unity, following a tutorial exactly but still had this issue.

The way I fixed this is just deleting this buggy script file and creating it again. It fixed my problem perfectly! Now I could add the script to my game object with no such errors!

Hope this helps you! Cheers!

Comments

0

I just ran into this same issue on Unity 2022.3.8f1. The project consisted only of some imported assets and 1 other script, so I was a little surprised when I created GameManager.cs and immediately ran into this issue when trying to apply it to a Game Object.

My issue was that my only other script had a missing semicolon, so it was throwing an error in the console. When I resolved that and saved the other script, I was then able to apply the GameManager script to my Game Object.

Comments

0

For anyone still having this issue, easy fix for me was to use Visual Studio Community, right click on top level Solution, and then do code cleanup under Analyze and Code cleanup option

Comments

0

I had the same error using Unity 2022.3.19f1 after importing a prefab from a project made in Unity 2021.3.5f1. Firstly, the scripts were missing so I copied them over. I tried dragging a script onto an empty game object with the same error. I then saw that there was an error in the console. It was another script that was missing and was referenced by the script generating the error. I copied the missing script over to the Assets directory and then it worked.

Comments

0

I solved it by adding the namespace to the classes for which it was reporting the error.

For example:

namespace PATHSUPERMAN_ADSAINITIALIZER { public class AdsInitializer : MonoBehaviour {

} }

Comments

0

I had this when trying to drag a component from a package.

  • The component(cs file) didn't show by clicking in "Add Component"
    • This is a red flag
  • Tried to add it by dragging from the Project tab (Packages/MyPkg/..)

Solution 1:

In the Project panel, right-click on the package folder Packages/MyPkg and reimport it.

PS: I got Debug Warnings: Importer(MonoImporter) generated inconsistent result for asset(...

Solution 2:

  • On Projects Panel:
    • Right-click Packages -> Reimport All

Now if you reimport only the Package, there are no warnings anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.