Script file names and namespaces
Understand how Unity matches script file names to class names, and use namespaces to prevent class name conflicts.
Read time 3 minutesLast updated 3 days ago
It's important to choose names for your script files and the classes declared inside them carefully. Use appropriate file names so that Unity can match each script file to its class, and organize your classes under namespaces to prevent class name conflicts.
Script file names
The file name you enter on creation of a script defines the name of the class 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 if the file name doesn't match. However, the following limitations apply:
MonoBehaviourScriptableObject- If your script defines multiple classes, Unity selects the class with the same name as the file.
- If you use the C# keyword to define a single
partial-derived class across multiple files, you can only use the file with the same name as theMonoBehaviourclass as a component.partial
Class names and namespaces
Naming conflicts happen when you declare multiple classes with the same name 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 character while another writes the equivalent code for the enemy. If both developers call their main class , the compiler can't determine which class each usage of in the code refers to.
ControllerControllerTo avoid class name conflicts, organize your classes under namespaces. A namespace is a collection of classes. The namespace prefixed to a class name provides a complete, unambiguous way to refer to the class. For more information, refer to Namespaces (Microsoft).
In the following example, the classes and are members of a namespace called :
Controller1Controller2Enemynamespace Enemy { public class Controller1 : MonoBehaviour { ... } public class Controller2 : MonoBehaviour { ... }}
To refer to these classes unambiguously in code, use their complete names. The complete name is the namespace plus the class name, which is and in the previous example. You can add a namespace declaration around existing class declarations, so you don't need to rename each class individually. You can add namespace declarations around classes wherever they occur, even if those classes are in different source files.
Enemy.Controller1Enemy.Controller2To avoid typing the namespace prefix repeatedly, add a directive at the top of the file:
usingusing Enemy;
The compiler then resolves references to and in this file as and . If the script also needs to refer to classes with the same name from a different namespace (for example, one called ), you must specify the namespace prefixes. If you import two namespaces that contain classes with the same name, the compiler reports an error when you use that class name without a namespace prefix. To resolve the conflict, use the full name, or declare a alias for one of the classes.
Controller1Controller2Enemy.Controller1Enemy.Controller2Playerusing