Namespaces
Tutorial
·
intermediate
·
+10XP
·
5 mins
·
(88)
Unity Technologies

Namespaces are a fundamental aspect of scripting. By the end of this tutorial, you’ll be able to:
- Explain the purpose of namespaces.
- Import a namespace.
- Declare a type as part of a namespace.
- Declare nested namespaces.
Languages available:
1. Overview
Namespaces are a fundamental aspect of scripting. By the end of this tutorial, you’ll be able to:
- Explain the purpose of namespaces.
- Import a namespace.
- Declare a type as part of a namespace.
- Declare nested namespaces.
2. Before you begin
This tutorial is part of Intermediate Scripting, a collection of basic introductions to a wide range of different programming concepts. If you want a guided introductory learning experience, try the Create with Code course or the full Junior Programmer pathway.
3. What are namespaces?
Namespaces group together type definitions in order to encapsulate them into systems that work together. You can think of namespaces like folders in an operating system. Types within a namespace can use each other freely, but types that are not in the same namespace must be imported before you can use them.
4. Conditions for using a type from a namespace
In order to use a type from a namespace when you write a script, you need to meet one of three conditions:
- Your script must contain a using directive for the namespace of the type that you want to use.
- When you use the type, you must also include its namespace.
- Your script must be part of the same namespace as the type that you want to use.
Include the using directive for the namespace of the type
This condition is the one most commonly met in a script. The correct syntax for this approach of including types from other namespaces is to include the keyword using at the top of your script, followed by the name of the namespace. Here’s an example:
In this code example, the UnityEngine namespace is included. This means all types from the UnityEngine namespace are available for use in this script. These include common types such as MonoBehaviour, GameObject, Transform, Rigidbody and many others.
Include the namespace when you use the type
You can meet this condition by adding the name of the namespace before types that use it, connected by the dot operator. Here’s an example that uses this technique:
Including the namespace when you use a type is useful if you only want to use types from a namespace once or if you want to specify the difference between two identically named types.
Write your script in the same namespace as the type
You’ll learn more about this final condition later in this tutorial.
5. Duplicated type names
All types from the same namespace must have different names. However, types from different namespaces can have the same name. For example, both the UnityEngine and System namespaces have definitions for a type called Random.
When you use a type with a name that exists in multiple namespaces, you must specify the actual type that you want to use. This can be done in two different ways:
- You can add the name of the namespace before the type, as you discovered in the previous step.
- You can add another using directive at the top of the script which specifies the default meaning of the type name throughout the script.
Here is an example of the second technique:
In this example, the Random type from the UnityEngine namespace is specified as what is meant whenever Random is used throughout the rest of the script.
6. Declare a type as part of a namespace
You can declare a type as part of a namespace by including the type declaration within a namespace block.
A namespace block has the following syntax: the keyword namespace followed by the name of the namespace. This name can be either an existing namespace or a new one. Here’s an example:
In this example, the PlayerMovement class is part of the Player namespace. In order to use the PlayerMovement type in other scripts, you would need to do one of the following three things:
- Write your other scripts in the same namespace (the Player namespace).
- Include a using directive for the Player namespace at the top of each script.
- Specify the namespace whenever you use the PlayerMovement type (Player.PlayerMovement).
7. Nested namespaces
You can nest namespaces like folders in an operating system. To access nested namespaces and types within them, use the dot operator. The dot operator is a period which indicates the nested relationship of the namespaces.
You can use nested namespaces to further organize systems and concepts within a project. For example, you could include all editor scripts in a parent namespace and then create nested namespaces for custom inspectors and various other tools.
Let’s review an example of nested namespaces for each of the three ways that you can use a namespace.
Nested namespace declaration
Nested namespace using directives
Nested namespace type usage
8. Summary
In this tutorial, you reviewed the basics of using namespaces and their role as an organizational tool for types.
If you are near the beginning of your game development journey, namespaces might feel unnecessary. However, they are a useful tool and you should keep them in mind as your projects become larger and more complex.