自定义类说,当它不应应

发布于 2025-01-21 21:16:46 字数 2086 浏览 1 评论 0原文

我有一个名为单元格的类,该单元源自图像。 我也有一个名为字段的类,其中我有一个单元格[] []名为单元格;

问题在于检查单元格中的单元格中的null是否在makefield()中。 第一个呼叫单元为null,因此没有问题,所有单元都被放入阵列中。 当调用makefield()再次等于null时,即使我可以在第一个呼叫中设置它们,我也看不到调试中的任何值。

public class Field
{
    Cell[][] cells;

    public Field(byte fieldSize, GameObject prefab, GridLayoutGroup grid)
    {
        MakeField(fieldSize, prefab, grid);
    }

    public void MakeField(byte fieldSize, GameObject prefab, GridLayoutGroup grid)
    {
        if (cells != null)
            ClearField();

        cells = new Cell[fieldSize][];

        RectTransform rt = grid.GetComponent<RectTransform>();
        ushort size = (ushort)(Math.Abs(rt.rect.width) / fieldSize - grid.spacing.x * (1 -  1 / fieldSize));
        grid.cellSize = new Vector2(size, size);

        for (byte x = 0; x < fieldSize; x++)
        {
            cells[x] = new Cell[fieldSize];
            for (byte y = 0; y < fieldSize; y++)
            {
                cells[x][y] = GameObject.Instantiate(prefab, rt).GetComponent<Cell>();
            }
        }
    }

    public void ClearField()
    {
        Debug.Log("Clearing field");
        for (byte x = 0; x < cells.Length; x++)
            for (byte y = 0; y < cells[x].Length; y++)
                cells[x][y].DestroyCell();

        Array.Clear(cells, 0, cells.Length);
    }
}

public class Cell : Image
{
    byte x, y;
    ushort value;

    public Cell(byte x, byte y, ushort value)
    {
        this.x = x;
        this.y = y;
        SetValue(value);
    }

    public void SetValue(ushort value)
    {
        this.value = value;
    }

    public void DestroyCell()
    {
        Destroy(gameObject);
    }
}

这是第一个通话结束时的阵列

这是第二个通话开始时的数组

为什么细胞为无效?

I have a class named Cell which derives from an Image.
I also have a Class named Field in which i have a Cell[][] named cells;

the problem is with checking if cells != null in MakeField().
the first call cells is null so no problem and all the cells get put in the array.
when calling MakeField() again cells is still equal to null and i can not see any values in debug, even though i can see them being set in the first call.

public class Field
{
    Cell[][] cells;

    public Field(byte fieldSize, GameObject prefab, GridLayoutGroup grid)
    {
        MakeField(fieldSize, prefab, grid);
    }

    public void MakeField(byte fieldSize, GameObject prefab, GridLayoutGroup grid)
    {
        if (cells != null)
            ClearField();

        cells = new Cell[fieldSize][];

        RectTransform rt = grid.GetComponent<RectTransform>();
        ushort size = (ushort)(Math.Abs(rt.rect.width) / fieldSize - grid.spacing.x * (1 -  1 / fieldSize));
        grid.cellSize = new Vector2(size, size);

        for (byte x = 0; x < fieldSize; x++)
        {
            cells[x] = new Cell[fieldSize];
            for (byte y = 0; y < fieldSize; y++)
            {
                cells[x][y] = GameObject.Instantiate(prefab, rt).GetComponent<Cell>();
            }
        }
    }

    public void ClearField()
    {
        Debug.Log("Clearing field");
        for (byte x = 0; x < cells.Length; x++)
            for (byte y = 0; y < cells[x].Length; y++)
                cells[x][y].DestroyCell();

        Array.Clear(cells, 0, cells.Length);
    }
}

public class Cell : Image
{
    byte x, y;
    ushort value;

    public Cell(byte x, byte y, ushort value)
    {
        this.x = x;
        this.y = y;
        SetValue(value);
    }

    public void SetValue(ushort value)
    {
        this.value = value;
    }

    public void DestroyCell()
    {
        Destroy(gameObject);
    }
}

this is the array at the end of the first call
enter image description here

this is the array at the start of the second call
enter image description here

why is cells null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文