Can't add script component because the script class cannot be found
Can't add script component 'X' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.
Unity could not find a MonoBehaviour class in that file, so it has nothing to add. Either the project does not compile, or the class name does not match the file name exactly.
Read the Console line first
X is the script Unity tried to add. Open the Console's error filter first: any red compile error anywhere in the project blocks every new script.
Common causes
There is a compile error somewhere
Unity only adds scripts after the whole project compiles. A red error in an unrelated file still blocks this one. Fix every error in the Console first.
The file name and class name differ
PlayerMovement.cs must contain public class PlayerMovement. The match is case-sensitive, and a renamed file does not rename the class inside it.
The class is not a MonoBehaviour
Only classes that derive from MonoBehaviour can be added as components. A plain class or a ScriptableObject cannot.
The script is in an Editor folder
Scripts inside any folder named Editor compile into the Editor assembly and cannot be added to GameObjects.
The fix
// File: PlayerMovement.cs
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
}Stop it coming back
Rename scripts from your code editor's rename refactoring, which renames the class and the file together, or rename both by hand at the same time.
Questions
- The file and class names match. Why does it still fail?
- A compile error anywhere in the same assembly stops every script in it from loading, even ones that are fine. Clear the Console and fix the first red error. Also check that the class is not abstract or generic: Unity cannot add either as a component.
- Does the case of the name matter?
- Yes.
PlayerController.csmust containclass PlayerController, with the same capital letters. On Windows and macOS the file system ignores case, but Unity does not. - Can a namespace cause this?
- No. A class inside a namespace is fine as long as the file name matches the class name. Namespaces are a good way to avoid duplicate class names between your code and imported assets.
Related errors
Got a different error? Paste it into the error lookup.