using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainCode : MonoBehaviour {
public static void Test() { }
}
MainCode.cs is no namespace
//---------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Game.Code {
public class Test : MonoBehaviour {
// Start is called before the first frame update
void Start() {
MainCode.Test(); //----------- Error
}
}
}
In Test.cs, how can I access the MainCode function? MainCode should be in unity default namespace, but how to access it?
Form the above code. In Test.cs, class MainCode is not found.
If I define the class MainCode in a namespace said âSomeLibâ, and on Test.cs include âusing SomeLibâ. I can find class âMainCodeâ in Test.cs
Another method is , remove class âTestâ from namespace âGame.Codeâ, to no namespace. It also can access the class âMainCodeâ in Test.cs.
So, how can I access the code from no namespace defined class? Many code download from asset store is no namespace using. eg NGUI. If I use namespace on my code I cannot access their code.
It probably comes down to assembly definitions (which are a requirement in packages, I believe).
If your code doesnât use assembly definitions, it will need to use them in order to use any other code that uses them. If you are, and the package isnât, you may have to introduce them into the package.
It should not cause by assembly definition, I have tried pack them all in Assembly-CSharp.dll, but no different.
If the class do not define in any namespace, it seems cannot be accessible from outside.
You only can use it from the class as same as to not define the class in any namespace.
Unity should give a special namespace name to that kind of class. But no information on it.
I found the solution. It should not case by namespace problem only. It also case by both namespace and assembly definition.
Create a two new assembly definition, can solve the problem. Because you cannot reference the default âAssembly-CSharp.dllâ, you cannot find it on âAssembly Definition Referencesâ.
Small correction. Assembly definition assemblies can opt to be Auto Referenced which means the predefined assemblies like Assembly-CSharp can access them (otherwise, they canât).
But yeah. OPâs issue with types in the global namespace is not a namespace problem as you said, as itâs not to do with the type being in the global namespace in the first place.
It helps when the type is shadowed by another type with the same name in the namespace / any parent namespace or when thereâs ambiguity with another using statement, or to fully qualify something. It doesnât seem applicable here assuming OP doesnât have types with the same name - if they did, the issue would then have been code grabbing the wrong type.