cant add script unity что делать
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.
8 Answers 8
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.
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
Introduction
In this article we look at the causes of the “Can’t Add Script” error in Unity in order to understand why it occurs and identify what actions to take to fix it.
Why do the “Can’t Add Script” Error occur in Unity?
This error in which we are not allowed to add a Script or component to a GameObject 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 extension “.cs” does not match the name of the programming class that is defined inside it. This usually happens when we create a Script and then change the name of the file without doing a Refactoring process.
To solve this problem first we must identify the Script that has conflicts, this can be done with the information provided by the “Can’t Add Script” error window that appears when we try to add the Script with conflicts to a GameObject. If you do not remember which Script produced the error, you can create an Empty GameObject and start adding each of the Scripts until the error appears.
Once we know which is the Script that generates the “Can’t Add Script” error, we open it and we locate the part of the class definition, which begins as follows:
public 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 modify the name of the class to match the name of the file, or modify the name of the file to match the name of the class.
Once this problem is solved it will be possible to add the Script to a GameObject. If you need to rename the Script the best way is to right click on the “NameOfTheClass” name and then click on “Rename”, this will make Unity modify that name in all the places where that Script is used.
Case 2: The script was created having errors in the console
The second reason why the “Can’t Add Script” error occurs in Unity is when new Scripts are created while there are errors in console, in general these are syntax errors (errors in writing the code). The reason in this case is that if there are errors in console, Unity can not compile, therefore if we create a new Script at this point, even if the file has been created, the programming class can not be added to the engine, in other words Unity will not know that this Script exists until it compiles.
The solution in this case is to solve all the errors in the console, this will make Unity compile and register the new Script. From that point on you will be able to assign the Script to a GameObject.
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.
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.
Not the answer you’re looking for? Browse other questions tagged c# unity or ask your own question.
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 © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.11.5.40661
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
why getting «Cant add script behavior AssemblyInfo.cs» in unity3d
I am trying to add a script with my object in unity. But it seems «Cant add script behavior AssemblyInfo.cs. The script needs to derive from MonoBehavior». How can I fix the problem please..
I have already checked the script and class name which are same already.Moreover I’ve copy and pasted the code in another file but it didn’t fix.
5 Answers 5
This happens because your script is treated to be not belonging to the project. In Visual Studio you’ll find that your script would be labelled as Miscellaneous file.
This is typically caused when a script is created outside of the Unity project. If you created the script with visual studio, file explorer, or simply copy/pasting the file, it may not be included in the Unity project.
Rather than try to re-build the assembly, simply create a new «PlayerMovement.cs» script using the Unity editor (right-click > create script) and write your code in that file.
It is a common issue and can be solved if you rename your c# script to PlayerMovement.cs and then build the solution again. No need to create a new script.
I think I’ve gotten a special case for this error. No common solution worked or applied for me, but I was able to solve it in an unexpected way.
To make the story short, the problem was that the path of my project was too large. (like, above 250 chars long, approx)
So, you could try moving your project to some short-pathed-place like your desktop (or renaming folders to make the route shorter) and, after that, delete your old script and creating a new one. (That worked for me)
For more info about this problem and the solution, take a look at this question and its first comment: DirectoryNotFoundException for an existing file path on Unity startup
Note that if you aren´t having ^ that error, this answer probably won´t help you. How to know if you have it? Check the console or just the bottom part of Unity when you open it to see if there is some error like that one.