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.
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