-1

This my be basic, but personally, I am stumped.

I am taking over a C# project that was previously not very well organized. Namespaces are pretty scattered throughout the code. This, to be specific, is a Unity project, but this is really just a C# issue.

There is a type (or class) in a file deep within nested directories that cannot be referred to in another file elsewhere in the project.

If the class were MyClass, it would look like:

File 1
------

public class MyClass
{
    // Stuff inside.
}

and

File 2
------
using ManyThings;

namespace SomeNamespace.NestedNamespace { // These came before me.

public class SomethingElse
{
    private MyClass _myClass; // <-- the type "MyClass" cannot be found.
}

}

So I (naively) thought that maybe if I added a namespace around file 1, it would help:

File 1
------

namespace FindMe
{

public class MyClass
{
    // Stuff inside.
}

}

and

File 2
------
using ManyThings;
using FindMe; // <-- The namespace FindMe cannot be found.
              // If I name it global::FindMe, it changes nothing.

namespace SomeNamespace.NestedNamespace {

public class SomethingElse
{
    private MyClass _myClass; // <-- the type "MyClass" STILL cannot be found.
}

}

I also tried something like global::FindMe, just in case. But it was not found either. Rider tells me: Cannot resolve symbol 'FindMe'. Unity tells me: The type or namespace name 'FindMe' could not be found in the global namespace (are you missing an assembly reference?).

I must be missing something obvious, but what?

Furthermore, in Rider, the contents of the "Refactor" menu are pretty much all greyed out when I try to use it on the class MyClass. I don't know if that's significant. I tried restarting after invalidating the cache.

Content of the Refactor menu in Rider, effectively all greyed out.

Again, this is a huge project, with many unused files. I'm trying to clean it up. This class needs to be kept. This is why I'm not sharing the actual code. It looks... icky.

But what am I missing?

eje211
  • 2,385
  • 3
  • 28
  • 44
  • Are you mixing editor code with runtime code? Is the file in a different assembly? – Retired Ninja Apr 27 '23 at 10:04
  • Reading the answer below, I realize that I didn't know that, in C#, projects and solutions were a different things. It's very late here. I'll look this up again tomorrow and try to add a project to the solution, now that I know how they work. – eje211 Apr 27 '23 at 11:39
  • @RetiredNinja Neither of these classes use editor code, but even if they did, the problem would occur at runtime, if I were to build the project in Unity, wouldn't it? The namespace would still be visible. – eje211 Apr 27 '23 at 11:40
  • Not necessarily. Editor code can see runtime code, but runtime code can't see editor code. The assembly they are part of depends on if they are in or under a directory named editor or not. If you have multiple assemblies then you'll need to find the asmdef and set the dependencies properly. You need to solve the problem in Unity since it is what generates the project you're using in Rider. If you fix it in Rider it won't matter to Unity and it will not compile in Unity. – Retired Ninja Apr 27 '23 at 12:57

1 Answers1

-1

This should work, as long as you are in the same project.

if you are not in the same project but same solution you can add dependency from one project to the other like so:

Right click in the project explorer dependencies, add project dependencie, select your project

this way you can access the namespace in a different project. ´

also be aware that nested namespaces work the same way as namespace declaration with periods.

    namespace SomeNamespace.FindMe
    {
        ...
    }

is the same as:

    namespace SomeNamespace
    {
        namespace FindMe
        {
            ...
        }
    }

hope this helps

Verwey
  • 51
  • 6
  • Not terribly useful in Unity as the projects are generated as needed and changes like this will be lost. – Retired Ninja Apr 27 '23 at 10:18
  • You may be right i didnt work too much with unity yet... you can still open unity projects in vs and check this way if this is the problem. – Verwey Apr 27 '23 at 10:23
  • Like I said above, the obvious thing I was missing was the relationship between project and solution in C#. I knew it had to be something obvious like that. I'll try to fix it tomorrow with this new information in mind. – eje211 Apr 27 '23 at 11:41
  • @Verwey I'm not on Windows. That's one of the reasons I use Rider. I don't have access to Visual Studio. (I'm guessing that "vs" is Visual Studio.) – eje211 Apr 27 '23 at 11:44