Unity 在编译有效的 C# 时遇到问题 class

Unity is having trouble compiling a valid C# class

我现在正在开发一个简单的游戏,Unity 给我一个奇怪的错误。我有一个名为 Level 的 C# class,它有一个带有四个参数的构造函数:

using System;

namespace World
{
    public class Level
    {
        public Grid grid { get; private set; }
        public int starty { get; private set; }
        public int endy { get; private set; }

        public Level(int width, int height, int starty, int endy)
        {
            this.grid = new Grid(width, height);
            this.starty = starty;
            this.endy = endy;
        }
    }
}

还有一个名为 WorldGen 的 class,但仍然没什么大不了:

using System;
using World;

namespace WorldGen
{
    public class WorldGen
    {
        public Level GenerateLevel(int width, int height, int starty, int endy)
        {
            Level level = new Level(width, height, starty, endy); // Line 47

            Grid grid = level.grid; // Line 49

            // ...

            return level;
        }
    }
}

这对我来说真的很简单(并且尚未在项目的任何地方调用 GenerateLevel)。在 PowerShell 中调用 csc 会编译目录中的所有文件,没有任何错误或警告。

但在 Unity window 中,我遇到了对我来说意义不大的错误:

Assets/WorldGen/WorldGen.cs(47:65): error CS1729: The type 'Level' does not contain a constructor that takes '4' arguments Assets/WorldGen/WorldGen.cs(49:31): error CS1061: The type 'Level' does not contain a definition for ''grid' and no extension method 'grid' of type 'Level' can be found (are you missing a using directive of an assembly reference?)

这就像 WorldGen 可以访问 class Level 但看不到它的任何 public 方法。

在我看来,Unity 只是错误地处理了我的代码,但根据经验,这种情况很少见。我只是不知道它想从我这里得到什么。 Level 在项目目录中。它的构造函数有 4 个参数,我将它传递给它所需的类型,并且它有 public grid 字段。让我明白的是,我知道当取消统一时,这段代码会自行编译。

我不知道我错过了什么,我想知道是否有人可以给我线索。

我会说你和 Level 有命名冲突。尝试明确指定命名空间:

World.Level level = new World.Level(width, height, starty, endy);