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?

3 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.

3 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.

3 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

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”.

8 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

2 Likes