C# NameSpace in Unity3D

NameSpace allows users to distinguish and organize scripts from one another. You can have two scripts with the same name without any naming confliction. Code can be easily imported into projects regardless of its current stage of development with NameSpace.

Demo

Creating 2 Scripts named weapon and using both on one Game Object.

by Default, unity uses three name spaces so we can use monobehavior methods and other methods we use to create our games.

These two scripts are both named weapon and are on one game object. Whats separating them from one another is their namespace.

name space simply wraps over the entire class to be able to give it a name space.

To access a variable or function from either script, I would insert their name spaces above. Just like unity name spaces.

Another use for this is to be able to extend a script for greater functionality. An example here would be making an ammo system for the PicoWeapon.

once using PicoWeapons is declared above. I can reference the script like any other script. in this case; Picoweapon.Weapon Picowep;.

the reference here gets assigned a game object that has the attached script. It will get the component of the script ,’Weapon’ so that we can access the Ammo int variable.

The Ammo variable is incremented when the attached game object collides with a trigger. If the collider has a tag named ,’Pico’. Then The ammo variable in the weapon script will increment.

Conclusion

Namespaces are particularly useful when working in teams or distributing your content. This is a great way to avoid script conflicts with having the same name. Namespaces allows assets to be imported seemly regardless of what stage of development the project is in. Additionally its another way to extend the functionality of other scripts.

--

--

--

Unity Developer Based out of California

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating Kubernetes (k8s) cluster by joining Google Cloud Platform (GCP) Virtual Machines

Long Live Terraform and 0.12!

Download Analysis Toolpak Excel Mac

Life is a journey of twists and turns, peaks and valleys, mountains to climb and oceans to explore.

A gentle introduction to Microservices

AMA 에어드랍 이벤트

Swift Dependency Management for iOS

Code Signing iOS app extensions on Continuous Integration Server

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
Vixian Phimmasone

Vixian Phimmasone

Unity Developer Based out of California

More from Medium

Var datatype in C#

Unity C# variables

Unity3D: Variables in C#