Unity Namespace

In by John FrenchPublished

Namespaces in Unity are collections of classes.

They are used to organise code into separate directories, and allow the compiler to differentiate between classes with the same name.

This is useful for keeping the code that you write separate from Unity’s core classes, and from installed third-party content, such as assets.

To access a class that is contained in a namespace, simply type the namespace name followed by the class that you want to use or, more commonly, include the namespace with a using directive.

To create your own namespace wrap your class in a namespace body with the name that want to use.

Like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameDevBeginner
{
    public class Player : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {

        }

        // Update is called once per frame
        void Update()
        {

        }
    }
}

It’s generally good practice to place your code inside of a namespace for the purpose of avoiding conflicts with other classes, especially if you’re using general class names that may already exist elsewhere.

This is especially important when creating an asset that is sold on the Unity Asset Store, as customers will need to be able to install the package without its contents conflicting with other scripts.

See also: