- Home /
Question out of Curiosity involving namespaces
Why do we need to state the individual namespaces we want to use in our scripts? Why can't they all just be built into each script?
What I'm referring to are things like these:
using UnityEngine;
using System.Collections;
etc.
etc.
Answer by troien · Apr 22, 2016 at 05:16 PM
You don't want to include all namespaces in your build. Because this would be a huge build. of which you wouldn't use most. Which is just a waste of disk space.
Define "all". Your scripts are for instance also in a namespace (If you don't define it somewhere it will be in the default global namespace). When you use a plugin which is made by someone else, the scripts in this plugin are also in a namespace. So 'all' could theoratically be a whole lot more then you might expect :D
Sometimes multiple namespaces contain classes with the same name. A good example of this is the Object class, which exists both in UnityEngine and System (but are 2 different classes). If you would include all namespaces, you would get a lot of conflicts like these.
So for example:
using UnityEngine;
using System;
public class Example : MonoBehaviour
{
public Object something1; // Not allowed, which object do you mean?
public UnityEngine.Object something2; // allowed
public System.Object something3; // allowed
}
And I probably forgot some reasons :p
See also : MSDN
Indeed, when you use other class, like through inheritance, the namespace is often already used/imported. You'd find a lot of warning thrown at you a lot and you'd end up going in circles trying to delete or leave in the best placed ones. It's better to just put them in yourself as you need them.
So basically, if all of the namespaces that have ever existed were placed on the same script, we're talking about terabytes of disk space being used up O.o