- Home /
can't add script, Unity says class name doesn't match
Trying to put a script on an object, Unity refuses with:
Can't add script RingPlaneController. The scripts file name does not match the name of the class defined in the script!
The script is in a file called RingPlaneController.cs - here are the first few lines
using UnityEngine;
using System.Collections;
public class RingPlaneController : TVWMeshGenerator {
TVWMeshGenerator subclasses from MonoBehavior
Since the filename does in fact match the class name, the error message must mean something else, but what?
Hmm... check capitalization? You might try copy-pasting both names into another text editor or diff tool, just to be sure that they're exactly the same.
Answer by icemacsea · Sep 11, 2014 at 02:16 PM
I just ran into this problem with javascript script. There's no way to get the classname wrong on a .js script, because the compiler just takes the name of the script as the classname! So I knew that it was something else. In my case, it turned out to be a C# script that wasn't compiling because of an error. Once I deleted that script, I was able to add my .js script to a gameObject with no problem.
Answer by Jessy · May 11, 2013 at 02:02 AM
You spelled MonoBehaviour with the American spelling; i.e. no u.
Ha ha. But I'm compiling here in Silicon Valley, so it still counts.
If you're actually using "$$anonymous$$onoBehavior", it's not going to compile. I know of several issues that cause this problem, but unless you edit your question to show that this simple fix isn't the problem, I won't offer anything. What you've written so far is confusing because of spelling. Also, use namespaces. The Asset Store is full of packages with these stupid initialisms.
Whoa, you're right! That spelling thing is just the kind of thing that slipped by an American. What's even more strange is that like probably most people, the script was set up by copying and pasting from other scripts. That script was actually a month old, I wonder how it ever got through. So thanks, fixed that, but - the problem persisted. After more work, I'm convinced it has to do with all the temporary files Unity generates. I made a simple edit to the script to force a recompile and then Unity was happy. I've tried it a couple more times, and each time forcing a recompile got rid of the problem.
Answer by shroge · Sep 03, 2014 at 05:14 PM
I ran into the exact same problem, however, I had no MonoBehavior/MonoBehaviour spelling issues, the names were in fact identical. It seemed to stem from creating a script from the asset panel. I was following this tutorial: https://www.youtube.com/watch?v=7GgbblB2Hmo#t=16 and when I got to the 2 min mark (I was following the exact click, drap, drop sequence that the tutorial showed) I received the same Class Name Doesn't Match error.
My fix was to simply restart Unity and it worked fine after that. This leads me to believe that there was some temp file messup on Unity's part.
Answer by SamTam · Feb 26, 2016 at 08:19 PM
Hello guys, i have read all you're problem's, and i wanted to help you guys. Read this carefully. First off look at the console and look if there are any errors(if it hasn't got any errors than look at the last script that you made, that one before the error). Then open the script and (Ctrl + A) copy it and then make a new text folder on you're desktop(or anywhere as long as you can find it) on the text folder paste the hole script, then save it. Go back to Unity and then delete the hole script(make sure you pasted it on the text folder) and then make a new one (if it's a C# script make sure to name it the same as you're last script, the one you coped) paste it in the script and then save it. It works for me.
I really hoped i helped you.
Answer by MrBadDragon · Mar 08 at 09:45 AM
Error message means that the name you have saved the file under does not match the class name.
Class MyClass needs to be saved in a file MyClass.cs
Your answer
Follow this Question
Related Questions
All scripts come up with compiler errors 3 Answers
Compiler errors 2 Answers
Compiler errors when opening unity 0 Answers
My server wont start! 0 Answers
I have a problem with OnTriggerEnter 2 Answers