Version: Unity 6.0 (6000.0)
Language : English
Introduction to programming in Unity
Create scripts

Script file names and namespaces

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 MonoBehaviour and ScriptableObject, Unity can still resolve a class type if the file name doesn’t match. However, the following limitations apply:

  • If your script defines multiple classes, Unity selects the class with the same name as the file.
  • If you use the C# partial keyword to define a single MonoBehaviour-derived class across multiple files, you can only use the file with the same name as the partial class as a componentA functional part of a GameObject. A GameObject can contain any number of components. Unity has many built-in components, and you can create your own by writing scripts that inherit from MonoBehaviour. More info
    See in Glossary
    .

Note: In all cases where a class name can’t be unambiguously matched to a particular file, Unity displays a warning.

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 Controller, the compiler can’t determine which class each usage of Controller in the code refers to.

To 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 Controller1 and Controller2 are members of a namespace called Enemy:

namespace 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 Enemy.Controller1 and Enemy.Controller2 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.

To avoid typing the namespace prefix repeatedly, add a using directive at the top of the file:

using Enemy;

The compiler then resolves references to Controller1 and Controller2 in this file as Enemy.Controller1 and Enemy.Controller2. If the script also needs to refer to classes with the same name from a different namespace (for example, one called Player), 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 using alias for one of the classes.

Additional resources

Introduction to programming in Unity
Create scripts