Can’t add script component

Because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match

I have 100% made sure the names match. I never changed the names to begin with. I can not resolve this to add scripts to anything inside unity. Please help.

11 Likes

So, the script name matches exactly? And not something like PlayerManager and playerManager?
The script inherits from Monobehaviour?
And you’re not seeing any compile errors in the console? (this is possibly your issue if you can’t add any scripts to gameobjects)

5 Likes

You need to fix any compile errors in your code before Unity can compile any changes to code (or new scripts) that have been made. Unity has to have compiled your scripts before it will be able to add them as components of GameObjects.

What errors does your console say you have?

4 Likes

No errors in console. File name and class match. I’m trying to get my son started on creating his first game in Unity, he’s 11. I made a simple controller for his character and it’s named “PlayerMovement” class is “PlayerMovement” as well. I’ve tried renaming, restarting, recreating the controller entirely. Nothing seems to be working. This only started after the new update. I even uninstalled and reinstalled Unity. I really don’t know what else to do.

4 Likes

Can you post this PlayerMovement class here?

This is the new controller I just started again. I’m trying to show him how to test as he goes and what all this means. But as I try to attach it to his character I get the error. Nothing in console. Names match.

4 Likes

I’ve just never had this issue in the past and I feel like it’s something simple that I’m missing. And I know I’ll feel like an idiot when I find it, but man I’ve been at this for days now.

1 Like

Things to try:

  1. Make sure the script is not located in a folder called Editor.

  2. Copy the script physically away to another part of your hard drive, DELETE the copy in the project, then make a fresh blank PlayerMovement.cs script. If you can’t add that then the problem is somewhere else.

Are you sure you don’t just have console errors disabled in the console?

4 Likes

What @Joe-Censored means is this button in the upper right corner of your console window: make sure it is ON and does not show any errors:

(Same goes for Log, LogWarning, LogError, those three buttons)

4239121--377296--errors.png

5 Likes
"NullReferenceException" with Instantiate()
Compile error in new project
I keep getting an error!
Unity & visual studio : link is broken for debug.log
Script is not working...
Cannot see build logs in unity
all compiler errors have to be fixed you can enter playmode
Help me pls (916487)
How to create a new asset type deriving from PhysicsMaterial?
NullReferenceException in my player spawner somteimes triggers, sometimes doesn't depending on entry
Why are certain parameters not showing in the editor?
beginner problem? Unity won't build game, even no errors in the scripts
new input system switch
Failed asserts are not printing anything
Array not working (902580)
Unsure about configuring the console
How do i fix can't add script behaviour AssemblyInfo. The script needs to derive rom MonoBehaviour?
Script errors without scripts
Installing a script from Tutorial
"using System;" breaks Unity
All compiler errors must be fixed before playmode (What errors?)
I'm missing a UI button.
Debug.Log deosn't work when called inside Video.EventHandler
Unity says to fix compiler errors but there aren't any in the code
NullReferenceException with new input system
ArgumentException when I try to get TextField from GameObject
Scripts are no longer recognized when the project has more than one script
Need help with raycast logic
unity editor play mode not working
SceneManager.LoadScene(string) doesn't work
Scripts just stopped working
Add Force (Impulse) based on contact normal, ball bouncing off a bumper (round surface)
error spawning random prefabs (C# Unity)
Unity won't allow me to drop reference into instance
Why are none of my scripts working?
How do I drag prefab into image space in the inspector?
Unity 3D all scripts not working
Debug Log showing old logs that are no longer in the script.
Slider function
Change the cube's material color
Foreach loop iterating only once? (Should be 3)
Problem with script (890422)
I can't assign gameobject
The cs1022 error type or namespace definition, or end of file
Not sure why I have this error. Please help?
Note equip System
Sequential yield returns don't execute [SOLVED]
Update Bug
Unity not finding script but script names are identical
Compile errors, but no errors reported, nor in editor

There’s no console errors. Though I’m leaning toward the issue being with PC, I haven’t had this problem with MAC. May be an issue with my desktop in particular, I don’t know. But I’m giving up. Thanks for the input.

1 Like

One thing to try is export your assets as a unitypackage. Than start a new project and import the assets.

I know it sounds strange, but once we had an animation that no matter what we did was screwed up. Luckily it was a small project so I exported the scene, started a new project and suddenly there were no animation issues.

Doubt it’s a “pc” issue directly.

3 Likes

I had the same issue, in my case the problem was solved by deleting directory “Library\PackageCache”.

9 Likes

I had the same issue.
Problem was solved after installing required .NET SDK and runtime framework.

Unity 2019.2.15f1 requires .NET 4.7.1

Hope it helps.

2 Likes

I had the same problem. I found that some unrelated script had an error. Once fixed I could add scripts to object again.

3 Likes

sir can you tell exact location

In Unity where your project is located (outside of Unity!) there are the Assets and the ProjectSettings folders… Library is right alongside, and PackageCache is underneath Library.

2 Likes

If anyone still has this issue you might want to try:
Go to the “Window” menu
Then down to “Package Manager” and click
You will get a list of every package available. Change from all available to just in project to shorten the list
Next click on the “package manager UI” from the list
Finally click “remove” from the lower right corner

1 Like

Idk what is wrong but it still doesn’t work. Am i this stupid or is it just Unity? im literally trying to fix it for like 2 hours now and it is stil not working. Maybe my Visual Studio needs to be on the same disk as Unity or something but im going to stop trying…

This didnt work for me too(for Netcode simple example from Manual). I did this actually : i saved first wrong name of the script and opened it in vs 2019. Then i Renamed it in the editor(and again opened it in vs 2019) and it didnt work. Then i deleted C# script and created new one renamed it and then it all worked good. Basically it seems if it is renamed 2 times somehow system cannot find the file dont know why - bug probably.

So just create file rename it as u want and then open in vs 2019 and it works for me - i use latest .net, windows 10 => also both unity and projects are on the same disk etc though do not know is that related(other posters above wrote about this) - hope this helps

3 Likes