Home > Back-end >  Unity Topology & CS0101
Unity Topology & CS0101

Time:05-03

Hey, so I've stumbled upon this little logistical issue. I can't find any actuall solutions to this specific use case, so I've created this simple example:

Imagine a simple sketch of a UI element in a game.

enter image description here

As you can imagine, this panel shows a popup that shows all the rewards and keys needed to unlock the chest.

It consists of multiple elements, and as these elements are repeated, it's a good practice to separate them. So let's say I Separate them into three different Monobehaviour classes, each having its own file.

  • TreasurePanelUI: The main panel element that houses references to all non-repeating UI elements.
  • Reward: A sub-element of the TreasurePanelUI, it houses references to its own elements (Image of the item, Text reference for the name, etc.)
  • Lock: A sub-element of the TreasurePanelUI, it houses references to its own elements (Image of the Lock, effects, animations of unlocking the lock, etc.)

So here comes the issue, how do we divide their namespaces? Well, here is my idea:

The dominant TreasurePanelUI may look something like this:

namespace UI //The class is a UI element.
{
    public class TreasurePanelUI: MonoBehaviour
    {
        //Neat code...
    }
}

And the other two elements may look something like this:

namespace UI.TreasurePanelUI //The class is a UI element but also part of TreasurePanelUI.
{
    public class Reward: MonoBehaviour
    {
        //Neat code...
    }
}

and

namespace UI.TreasurePanelUI //The class is a UI element but also part of TreasurePanelUI.
{
    public class Lock: MonoBehaviour
    {
        //Neat code...
    }
}

Seems logical right? Well, here's the problem: It won't compile. This will throw a CS0101. Meaning our class name is clashing with the namespace name.

My main question is, is there an alternative way of achieving the same code topology that will actually compile?

Also, feel free to point out if my approach or my topology/method is entirely wrong! haha

CodePudding user response:

I don't think you should use the same names for classes and namespaces, because (like it happened to you) they might clash.

The C# doc comes very close to discouraging what you are doing.

DO NOT use the same name for a namespace and a type in that namespace. For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

Source

I would suggest to just put all the classes in the UI.Treasures namespace.

CodePudding user response:

You already got this answer when you asked this exact same question yesterday.

Your code does not run because UI.TreasurePanelUI is an ambiguous call - is it the namespace for the other classes or is it the TreasurePanelUI class?

Like I told you in your other question - just rename your TreasurePanelUI class.

If you're looking for a list of alternatives then I think that's off-topic, but you could make the other classes subclasses of your TreasurePanelUI class, like:

namespace UI //The class is a UI element.
{
    public class TreasurePanelUI: MonoBehaviour
    {
        //Neat code...
        public class Reward: MonoBehaviour
        {
            //Neat code...
        }
        public class Lock: MonoBehaviour
        {
            //Neat code...
        }
    }
}

Now you can access UI.TreasurePanelUI.Reward or whatever. If you don't want them all in the same file you can use the partial class, like:

namespace UI //The class is a UI element.
{
    public partial class TreasurePanelUI: MonoBehaviour
    {
        //Neat code...
    }
}
namespace UI //The class is a UI element.
{
    public partial class TreasurePanelUI: MonoBehaviour
    {
        public class Reward: MonoBehaviour
        {
            //Neat code...
        }
    }
}
namespace UI //The class is a UI element.
{
    public partial class TreasurePanelUI: MonoBehaviour
    {
        public class Lock: MonoBehaviour
        {
            //Neat code...
        }
    }
}

But really, the best thing you can do it to just change the name of your TreasurePanelUI class. If it's a base canvas or base element then you could have

namespace UI.TreasurePanelUI //The class is a UI element.
{
    public class BaseCanvas: MonoBehaviour
    {
        //Neat code...
    }
}
  • Related