Can't Add Script Component to Game Object

Yes this question has come up many times but I’ve yet to find a solution for me. In my Unity scene I created an empty GameObject. I created a C# script, renamed the script and the class name to match the script name. That’s it, I’ve made no other edits to said C# script. Yet I can’t even attach this script to the GameObject because of this error (see image). So…how do I add scripts to GameObjects?

Images showing error in Unity as well as the script in Visual Studio: Imgur: The magic of the Internet

Solved my problem. Uninstalled Unity Hub and downloaded Unity as its own client. Something about that Hub was screwing up the script compiling but doesn’t matter, scripts now attach to game objects.

Unity hub was not the issue. If you look at the editor, it has a compile time error. This is what keeps you from being able to add scripts to gameobjects. Also note that if there is an asterisk in the tab for a script in VS, that means you haven’t saved that script after you’ve changed something. But either way, in this case your DemoScript is the problem.

You forgot to save your script so the correct name wasn’t updated as long as you see the ‘*’ next to your script name the changes wont be loaded