- Home /
When exactly do scripts have to be named exactly the same as the class they contain?
Am I correct in thinking it is only when a C# script contains a static class?
No, that's not correct. Eric5h5 provides a good answer. It's for all classes that derive from $$anonymous$$onoBehavior (and possibly ScriptableObject and Editor classes).
Answer by Eric5h5 · Mar 03, 2011 at 10:28 AM
It's when you want to attach a script to some object. Static classes or not makes no difference.
Is that only in C#? I've been attaching differently named JS scripts to objects with no ill effects... And for instance, what if you want to attach 2 scripts to the same object?
Eric5h5 means that any scripts (classes derived from $$anonymous$$onoBehavior) need to be placed in a file with the same name as the class. You can add other classes to the file, given the first class is the one named after the file IIRC. If you write plain classes you don't need to name them the same, although it's good practice so you can navigate your source code more easily.
@Robin $$anonymous$$ing: In JS, a script is automatically a class with the same as the script name, so it's not an issue in the first place. With C#, you have to explicitly put a class in the script.
Ah, I see. Thanks... I'm starting to get my head round this now.
Answer by Demigiant · Mar 03, 2011 at 10:27 AM
Uhm, with C#, scripts should always have the class name equal to the filename (if that's what you mean), at least if you want to use them. Sorry if I misunderstood: this question puzzles me :P
No, you can use C# scripts which have a different class name compared to the file name, as long as you don't try to attach them to anything. (So they can't be $$anonymous$$onobehaviour scripts obviously.)
Interesting: didn't know about that :) Anyway, I wonder why somebody would want class names to be different from filenames: doesn't it get terribly confusing? Or is there some scenery in which it's useful?
There are instances where you want to go with partial classes, or nested classes which fall short of this category. Also, some people might want to bunch up a few helper classes close to the main class that use them. However, this enforcement of classname-to-filename in C# is something unique to Unity for me. I haven't seen this enforced in other areas regarding C#. I know Java follow this convention, but that's about it. I assume it has to do with compiling individual scripts, possibly something related to JS or Boo.
Answer by Parthon · Mar 03, 2011 at 01:19 PM
It's only C# objects that derive from MonoBehaviour that you want to attach to another object.
This is like 90% of the use of scripts anyways, so it's a good idea to name files after classes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The scripts file name does not match the name of the class defined in the script! 1 Answer
Is a clone object still a (clone) if you change the name of it using script? 1 Answer
Accessing another script from a class 3 Answers
Color set to black even though it's set by the editor 0 Answers
CoD 4 "weapon classes" 2 Answers