Как исправить ошибку can t add script
Перейти к содержимому

Как исправить ошибку can t add script

  • автор:

Как исправить ошибку can t add script

Reproduction steps:
1. Create a new script.
2. Change the filename of that script.
3. Add script to an object in the scene.

Expected error message: «Can’t add script component ‘NewBehaviourScript’ because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.»

Actual error message: «Can’t add script behaviour VisualContainerAsset. The script needs to derive from MonoBehaviour!»

Reproduced: 2018.1.0a5, 2018.1.0f1
Not reproducible: 2017.1.3p1, 2017.2.1p3, 2017.3.1f1, 2018.1.0a4, 2018.2.0b1

Cant add script component. Script Class cannot be found.

It’s a bit difficult to diagnose without seeing your code and directory structure of your Scripts folder, but I would start by double-checking that the script file and the class inside it are both named «RandomSoundPlayer» without any typos and that the class compiles without errors.

bryson thornton
bryson thornton

thanks. it worked

Tojo Alex
Tojo Alex

thanks i had same issue

Posting to the forum is only allowed for members with active accounts.
Please sign in or sign up to post.

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

Programmer's user avatar

Joe's user avatar

12 Answers 12

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.

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.

How to solve «Can’t Add Script» in Unity

In this arti­cle we look at the caus­es of the «Can’t Add Script» error in Uni­ty in order to under­stand why it occurs and iden­ti­fy what actions to take to fix it.

Dear read­er

In the chan­nel there lots of videos about Blender, Uni­ty and pro­gram­ming
in which we solve dif­fer­ent prob­lems and we pro­vide use­ful infor­ma­tion on these topics.

In the fol­low­ing video you can see a brief descrip­tion on how to solve the «Can’t Add Script» error in Unity:

ABOUT THIS VIDEO

In this video we see how to solve «CAN’T ADD SCRIPT» error in Uni­ty through code.

HERE MY UNITY PLAYLIST
����

Why do the «Can’t Add Script» Error occur in Unity?

This error in which we are not allowed to add a Script or com­po­nent to a GameOb­ject can arise for two reasons.

Case 1: The name of the script and the name of the class it defines do not match

The first one is that the name of the Script, that is to say, the name of the file with exten­sion «.cs» does not match the name of the pro­gram­ming class that is defined inside it. This usu­al­ly hap­pens when we cre­ate a Script and then change the name of the file with­out doing a Refac­tor­ing process.

To solve this prob­lem first we must iden­ti­fy the Script that has con­flicts, this can be done with the infor­ma­tion pro­vid­ed by the «Can’t Add Script» error win­dow that appears when we try to add the Script with con­flicts to a GameOb­ject. If you do not remem­ber which Script pro­duced the error, you can cre­ate an Emp­ty GameOb­ject and start adding each of the Scripts until the error appears.

Once we know which is the Script that gen­er­ates the «Can’t Add Script» error, we open it and we locate the part of the class def­i­n­i­tion, which begins as follows:

pub­lic class NameOfTheClass …

The name of the class is the name that has to match the name of the file, so what you can do is mod­i­fy the name of the class to match the name of the file, or mod­i­fy the name of the file to match the name of the class.

Once this prob­lem is solved it will be pos­si­ble to add the Script to a GameOb­ject. If you need to rename the Script the best way is to right click on the «Name­OfThe­Class» name and then click on «Rename», this will make Uni­ty mod­i­fy that name in all the places where that Script is used.

I can't add script in unity

I’m a beginner in unity so I got this problem. Here is the script:

I don’t know why but it keep saying that Can’t add script component ‘Player control’ because the script class cannot be found.Make sure that there no compile errors and that the file name and class name match.

House's user avatar

3 Answers 3

Check the file name of the script and make sure it’s the same as the class name. I’ve had this problem before after renaming a script through the editor.

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

You either have a compile error in this script, or you have one in a different script. If this is the first time you’re trying to use this script, the entire solution needs to compile before Unity knows about this new class you’re introducing.

Locate your error and correct it before trying to add this script. It could be a compile error or something like a name mismatch like SanSolo suggests.

House's user avatar

You must log in to answer this question.

    The Overflow Blog
Related
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *