Search Unity

Resolved How to use namespaces?

Discussion in 'Scripting' started by Monsterwald, Jun 6, 2022.

  1. Monsterwald

    Monsterwald

    Joined:
    Jan 19, 2020
    Posts:
    68
    I can find little to no useful information to namespaces. I need to reference variables in my managerscript alot and my whole structure is a very confusing and complicated cobweb of references, so I thought I have a bright idea and turn this class into a namespace, something like this:
    Code (CSharp):
    1. namespace example {
    2.     public class Something : Monobehaviour
    3.     {
    4.          public float numberA;
    5.     }
    6. }
    and from another script, I thought I could simply access the variables via:

    Code (CSharp):
    1. using example;
    2.  
    3. public class SecondScript : MonoBehaviour
    4. {
    5.     Something st;
    6.     float numberZ;
    7.  
    8.    private void Start
    9.    {
    10.       numberZ= st.numberA;
    11.       //numberZ = numberA not working either
    12.    }
    13. }
    which turns into a NullReferenceException error!? So... me very confused..
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Namespaces are purely for code organisation.

    You still need a concrete reference to other instances as per normal. In your second code's case,
    Something st;
    is never assigned hence being null.
     
    Bunny83 likes this.
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    I dont think namespaces are what you are looking for.

    Namespaces are used so you can safely use class names without overlap with other plugins or unity classes.

    E.g

    Code (CSharp):
    1. Namespace SpaceA
    2. {
    3.    Class ClassA
    4.    {
    5.    }
    6. }
    7.  
    8. Namespace SpaceB{
    9.    Class ClassA
    10.    {
    11.    }
    12. }
    13.  
    14. Class ClassB
    15. {
    16.    SpaceA.ClassA var1 = new SpaceA.ClassA();
    17.    SpaceB.ClassA var2 = new SpaceB.ClassA();
    18. }
    19.  
    What you are looking for is the singleton pattern, which is just a fancy term for a class with a singular instance.

    The simplest way to implement it in unity is as follows:

    Code (CSharp):
    1. Public class Something : MonoBehaviour
    2. {
    3.    Static Something m_instance;
    4.    Public int TestVar;
    5.  
    6.    void Awake()
    7.    {
    8.       m_instance = this;
    9.    }
    10.  
    11.    Public static Something GetInstance()
    12.    {
    13.       return m_instance;
    14.    }
    15. }
    16.  
    17. //to use it
    18. class OtherClass : MonoBehaviour
    19. {
    20.    Something m_something;
    21.  
    22.    void Start()
    23.    {
    24.       //safe to grab here since instance is set in somethings awake
    25.       m_something = Something.GetInstance();
    26.       m_something.TestVar = 5;
    27.    }
    28. }
    Something needs to be placed on an object in your scene & its only safe to use the GetInstance after Something Awake has been called. Thats why OtherClass does it in Start.
     
  4. Monsterwald

    Monsterwald

    Joined:
    Jan 19, 2020
    Posts:
    68
    Hmmm, okay, thanks, looks like I completely misunderstood what namespace are there for... I knew about singletons but never used them, since my projects aren't big. well I guess it's time to use them anyway.
    Edit: I still get NullReference Errors on some of my scripts, which apparently can't find the singleton... no clue why but I will glue something together... somehow...
    Edit2: Those scripts use onEnabled, that seems to be the issue.
     
    Last edited: Jun 6, 2022