Namespaces

Chris Didier
2 min readApr 6, 2021

--

Namespaces allow us to organize and maintain our code libraries.

If I was to create a default C# scripts in Unity I have 3 namespaces automatically provided to me.

Default Namespaces

The using UnityEngine namespace allows us to apply monobehavior to our scripts. Without it we would not be able to apply mono type scripts to our game objects.

Now if I want to have access into a different namespace I must include it in my code. If I wanted to add an image I must add using UnityEngine.UI; to be able to access the UI. If I wanted the ability to change a scene I must add the scenemanagement namespace.

Another great use if you want better management with IntelliSense which predicts what tools you want access to before you type them.

Now imagine if you are getting an asset from the asset store and you already have a class called weapon in your game. To resolve this issue we can create a namespace to remove a conflict that is created with two scripts having the same name. To do this post the class within this newly created namespace.

To be able to access this scripts from the already created weapons script I will want to create a new using WeaponPack;

You can go even farther and isolate if you had a specific weapon pack and say WeaponPack.Magic; if you only wanted to touch the magic weapons instead of all the weapons.

So if I were to create an asset and place it on the store it is good practice to put the asset in it’s own namespace to avoid any potential naming conflicts that might arise from your game if you are working on one.

--

--

Chris Didier
Chris Didier

No responses yet