Naming scripts
Name your scripts to help the compiler identify your classes. Use namespaces to logically organize your scripts and prevent class name conflicts.
Read time 4 minutesLast updated 7 days ago
It's important to choose names for your script files and the classes declared inside them carefully. Using appropriate file names and organizing your classes under namespaces helps ensure the compiler can identify your classes without errors.
Script file names
The file name you enter on creation of a script will also be used for the name of the class defined inside it. It's good practice for a script's file name to match the name of the class defined inside it.
For scripts derived from the built-in Unity types and , Unity can still resolve a class type defined in your script even if the file name doesn't match, but there are some limitations:
MonoBehaviourScriptableObject- If multiple classes are defined in your script, Unity selects the class with the same name as the file.
- If you use the C# keyword to define a single MonoBehaviour-derived class across multiple files, only the file with the same name as the
partialclass can be used as a Component.partial
Class names and namespaces
Naming conflicts arise when multiple classes with the same name are declared in different parts of your project. The likelihood of naming conflicts increases with the scale of a project and the number of contributors. For example, one developer might write code to control the main player character while another writes the equivalent code for the enemy. If both developers choose to call their main class , the compiler won't be able to determine which class usages of in the code refer to.
ControllerControllerTo avoid class name conflicts, it's good practice in C# programming to organize your classes under namespaces. A namespace is a collection of classes and the namespace prefixed to a class name provides a complete, unambiguous way to refer to the class. Refer to the Microsoft documentation on namespaces for more information.
In the example below, the classes and are members of a namespace called :
Controller1Controller2Enemynamespace Enemy { public class Controller1 : MonoBehaviour { ... } public class Controller2 : MonoBehaviour { ... }}
You can reference these classes unambiguously in code by using their complete name. The complete name is the namespace plus the class name, in this case and respectively. You can add the namespace declaration around existing class declarations so you don't need to change the names of all the classes individually. You can add namespace declarations around classes wherever they occur, even if those classes are in different source files.
Enemy.Controller1Enemy.Controller2You can avoid having to type the namespace prefix repeatedly by adding a directive at the top of the file.
usingusing Enemy;
This causes the compiler to resolve references in this file to and as and , respectively. If the script also needs to refer to classes with the same name from a different namespace (for example, one called ), then you must specify the namespace prefixes. If two namespaces that contain conflicting class names are imported with directives in the same file, the compiler reports an error.
Controller1Controller2Enemy.Controller1Enemy.Controller2PlayerusingLimitation for multiple namespaces
Unity has a specific limitation relating to namespaces and MonoBehaviour or ScriptableObject classes. If your file contains a definition for a MonoBehaviour or ScriptableObject class, you can't use multiple namespaces within that file.
Unity gives the following warning in the console:
Class MyClass can not exist in multiple namespaces in the same file, even if one is excluded with preprocessor directives. Please move these to separate files if this is the case.
If you have a file which defines a MonoBehaviour in one namespace, and other classes in a different namespace within the same file, Unity will not recognize the MonoBehaviour class and you will not be able to use it on GameObjects. This limitation was introduced in Unity 2020.1 to improve import and compilation speed, and therefore some older asset store packages written before this limitation was introduced may function incorrectly as a result. To fix problems relating to this issue, separate out the code for the classes in each namespace to separate files.