Search Unity

Resolved Contact from outside Namespace

Discussion in 'Scripting' started by renman3000, Mar 31, 2023.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    SOLVED:
    use: "using namespace"....




    Hi there,
    I have a third party asset, which uses a namespace in its code. I want to use one of its components but not have to change my entire project to be under this namespace.

    How can I access soemething in this namspace from soomething outside of it?
    Thanks
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    It seems you already figured it out yourself. Though just to be clear: The namespace of a class or type is part of the full name of that class / type. You can always use the full class name as well. Though it's of course much more convenient to "import" a namespace by adding a using statement at the top of your file. A using statement does just removes the need of specifying the full class name all the time.

    Note that namespaces has been invented to handle potenial name collisions between classes. Though you have to be careful with using statements. When you import two different namespaces and both contain a type with the same name, the compiler would throw an error since it doesn't know which type you meant. The go-to example in Unity would be the UnityEngine.Random class and the System.Random class. So when you import

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    you can no longer use the Random class (and some others may cause issues as well). As I said in the beginning, you can always use the full class name instead:

    Code (CSharp):
    1. System.Random rnd = new System.Random();
    instead of

    Code (CSharp):
    1. Random rnd = new Random();
    which would be ambiguous.