Namespace- C# Skill in Unity: 5

Objective: get to know the basic of namespace in Unity.

Namespace is a basic structure of C#. In Unity, you probably will not see this word too often like other basic structures such as class. However, it is everywhere when you creating or using a C# script.

Where is namespace

When you open a script at the first time using Unity, you already seen the namespace.
The top 3 lines of code is the namespace that this script had used.

The class MonoBehaviour is also under the namespace “UnityEngine”. If we delete or comment out the namespace, it should shows error under MonoBehaviour.

Missing namespace.

Namespace is a library that you can use the function in it. The rule of getting the access of that library is “using” it.
For example, you will not be able to use NavMeshAgent if you have not access the library of UnityEngine.AI, which is a namespace of AI.

Create your own namespace

When we download some assets from Unity Asset Store, there might be some conflict when the name of script is same as scripts that we already had. In this situation, we can create a new namespace to solve this problem.

Let’s assume that we create a script for Shield and also download a script called Shield. Open either script you will see the error sign like this.

Now we can create a name space and cut down the whole part of downloaded script and paste it in.

As you can see the error of conflict is gone.
And we can use the downloaded script by using the namespace we just created.

That is how to solve the conflict when downloaded a script with existed name in Unity.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store