How to access code on default namespace

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?

Nothing to do with namespaces. Test is a non-static method. You need a reference to a MainCode component in order to call the instance method.

Also please post code correctly next time.

public class ComponentOne : MonoBehaviour
{
	public void TestMethod() { }
}

public class ComponentTwo : MonoBehaviour
{
	[SerializeField]
	private ComponentOne _componentOne;
	
	private void Awake()
	{
		_componentOne.TestMethod();
	}
}

Sorry something typing wrong.

My question is the code do not include in any namespace. How can I access in other namespace code?

using ??? (what is the name of no namespace?)

And as I mentioned your error has nothing to do with namespaces.

You don’t need to include a using for types not in a namespace.

1 Like

You don’t understand my question.

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.

Once again, code in no namespace requires no using.

If you can’t access the code, something else is the cause, most likely assembly definitions (or lack thereof).

Namespaces are nothing but an organisational system. They do not change how code is compiled.

1 Like

I want to ask the method, how can I access it.

I know it should have something wrong. So how to solve?

On your own namespace class, how can you access NGUI 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.

Docs are here: Unity - Manual: Assembly definitions

Also note the order of special folders: Unity - Manual: Special folders and script compilation order

Code in ‘Plugins’ (Assembly-CSharp-firstpass) folder cannot reference code in the Assembly-CSharp assembly.

Once again, namespaces don’t change how code is compiled/referenced, but assemblies do.

1 Like

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

So basically what I said here:

1 Like

I have no idea if the following helps but apparently the global namespace can be referenced directly

static void Main() => global::System.Console.WriteLine("Using global alias");

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.

1 Like

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.