Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Namespaces not being recognized

I have the following class

namespace _Scripts.EnemyStuff
{
    public class Enemy : MonoBehaviour, IDamagable
    {
    }
}

And then when I try to use the class Enemy I need to specify that it comes from _Scripts.EnemyStuff even if they share the same parent.

The following code doesn’t detect the Enemy class. I need to specify EnemyStuff.Enemy in order to make it work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

namespace _Scripts.Managers
{
    public class EnemyManager : MonoBehaviour
    {
        public Enemy firstEnemyGO;
    }
}

Why is this happening? Also, why it doesn’t work if I use

using Enemy = _Scripts.EnemyStuff.Enemy;

>Solution :

They do not really

share the same parent

Your namespace hierarchy looks like

_Scripts
|-- EnemyStuff
|   |-- Enemy
|
|-- Managers
    |-- EnemyManager
    

c# looks for the type recursive up in the parent path (_Scripts.Managers.XY or _Scripts.XY) bu doesn’t bubble back down all possible sibling namespaces.

So yes in this structure you will need to provided the namespace and use e.g.

public EnemyStuff.Enemy firstEnemyGO;

the EnemyStuff actually can be found looking in the common parent namespace _Scripts so you can omit the _Scripts.


Either

using System;
using UnityEngine;
using Enemy = _Scripts.EnemyStuff.Enemy;

namespace _Scripts.Managers
{
    public class EnemyManager : MonoBehaviour
    {
        public Enemy firstEnemyGO;
    }
}

or

using System;
using UnityEngine;

namespace _Scripts.Managers
{
    using Enemy = EnemyStuff.Enemy;
    
    public class EnemyManager : MonoBehaviour
    {
        public Enemy firstEnemyGO;
    }
}

or

using System;
using UnityEngine;

namespace _Scripts.Managers
{
    public class EnemyManager : MonoBehaviour
    {
        public EnemyStuff.Enemy firstEnemyGO;
    }
}

work perfectly fine for me

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading