Ошибка в C# скрипте для Unity

Делаю roguelike в рамках курсовой. Впрочем, за проект я возьмусь и вне учебного плана. Написал код Binary Space Partitioning для генерации карты:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using Random = UnityEngine.Random;

public class BSP : MonoBehaviour
{
    public RuleTile tile;
    public Tilemap tilemap;
    public int mapWidth, mapHeight;

    private int[,] map;
    private List<Rect> rooms;
    private List<Hall> halls;
    private List<Leaf> leaves;

    public void Start()
    {
        rooms = new List<Rect>();
        halls = new List<Hall>();
        leaves = new List<Leaf>();

        map = new int[mapWidth, mapWidth];


        tilemap = BSPeniing(); // Ошибка

    }

    public Tilemap BSPeniing()
    {

        Tilemap tilemap = new Tilemap();

        Splitting(new Leaf(0, 0, mapWidth, mapHeight), 5, 3);

        map = mapping(rooms, halls);


        for (int i = 0; i < map.GetLongLength(0); i++)
            for (int j = 0; j < map.GetLongLength(1); j++)
                if (map[i, j] == 0)

                    tilemap.SetTile(new Vector3Int(i, j, 0), tile); // Ошибка

        return tilemap;
    }

    private int[,] mapping(List<Rect> rooms, List<Hall> halls)
    {
        int[,] map = new int[mapWidth, mapHeight];

        foreach (Rect room in rooms)
            for (float i = room.x; i < room.x + room.width; i++)
                for (float j = room.y; j < room.y + room.height; j++)
                    map[(int)i, (int)j] = 1;

        foreach (Hall hall in halls)
        {
            for (float i = hall.x1; i < hall.x2; i++)
                map[(int)i, (int)hall.y1] = 1;
            for (float j = hall.y1; j < hall.y2; j++)
                map[(int)hall.x1, (int)j] = 1;
        }

        return map;
    }

    private class Leaf
    {
        public float
            x, y,
            width, height;

        public Leaf(float X, float Y, float Width, float Height)
        {
            x = X; y = Y;
            width = Width;
            height = Height;
        }
    }

    private class Hall
    {
        public float
            x1, y1,
            x2, y2;

        public Hall(float X1, float Y1, float X2, float Y2)
        {
            x1 = X1; y1 = Y1;
            x2 = X2; y2 = Y2;
        }
    }

    private void Halling(Leaf leaf1, Leaf leaf2)
    {
        float
            x1, y1,
            x2, y2;

        x1 = leaf1.x + leaf1.width / 2;
        y1 = leaf1.y + leaf1.height / 2;

        x2 = leaf2.x + leaf2.width / 2;
        y2 = leaf2.y + leaf2.height / 2;

        halls.Add(new Hall(x1, y1, x2, y2));
    }

    private void Rooming(Leaf leaf)
    {
        Rect room = new Rect();
        room.x = Random.Range(leaf.x, leaf.x + leaf.width / 2);
        room.y = Random.Range(leaf.y, leaf.y + leaf.height / 2);
        room.width = Random.Range(leaf.width / 2, leaf.x + leaf.width - (int)room.x);
        room.height = Random.Range(leaf.height / 2, leaf.y + leaf.height - (int)room.y);

        rooms.Add(room);
    }

    private void Splitting(Leaf leaf, int iterations, int maxratio)
    {
        if (iterations == 0)
            return;

        int choise = Random.Range(0, 1);

        Leaf leaf1, leaf2;

        float
            sx1, sy1,
            sx2, sy2;

        if (leaf.width / leaf.height > maxratio)
        {
            sx1 = Random.Range(leaf.x + leaf.width / maxratio, leaf.x + leaf.width * (maxratio - 1f) / maxratio);
            sx2 = sx1;

            sy1 = leaf.y;
            sy2 = leaf.y + leaf.height;
        }
        else if (leaf.width / leaf.height < 1d / maxratio)
        {
            sx1 = leaf.x;
            sx2 = leaf.x + leaf.width;

            sy1 = Random.Range(leaf.y + leaf.height / maxratio, leaf.y + leaf.height * (maxratio - 1f) / maxratio);
            sy2 = sy1;
        }
        else if (choise == 0)
        {
            sx1 = Random.Range(leaf.x + leaf.width / maxratio, leaf.x + leaf.width * (maxratio - 1f) / maxratio);
            sx2 = sx1;

            sy1 = leaf.y;
            sy2 = leaf.y + leaf.height;
        }
        else
        {
            sx1 = leaf.x;
            sx2 = leaf.x + leaf.width;

            sy1 = Random.Range(leaf.y + leaf.height / maxratio, leaf.y + leaf.height * (maxratio - 1f) / maxratio);
            sy2 = sy1;
        }

        leaf1 = new Leaf(leaf.x, leaf.y, sx2 - leaf.x, sy2 - leaf.y);
        leaf2 = new Leaf(sx1, sy1, leaf.x + leaf.width - sx1, leaf.y + leaf.height - sy1);

        Halling(leaf1, leaf2);

        if(iterations == 1)
        {
            Rooming(leaf1);
            Rooming(leaf2);

            leaves.Add(leaf1);
            leaves.Add(leaf2);
        }

        iterations--;
        Splitting(leaf1, iterations, maxratio);
        Splitting(leaf2, iterations, maxratio);
    }
}

Но раз за разом вылазит одна и та же ошибка:

NullReferenceException UnityEngine.Tilemaps.Tilemap.SetTileAsset (UnityEngine.Vector3Int position, UnityEngine.Object tile) (at <44cfcfae157e424ba01348dcfebb2066>:0) UnityEngine.Tilemaps.Tilemap.SetTile (UnityEngine.Vector3Int position, UnityEngine.Tilemaps.TileBase tile) (at <44cfcfae157e424ba01348dcfebb2066>:0) BSP.BSPeniing () (at Assets/Scripts/BSP.cs:45) BSP.Start () (at Assets/Scripts/BSP.cs:29)

Сколько бы я там всё ни рассматривал - не могу понять, в чем проблема, а как трассировать код в unity (если такое вообще есть), я не знаю. Rule Tile можно заменить на обычный Tile (на тестовых скриптах у меня все те же методы работали и с теми, и с другими). Прошу помощи: объяснить, в чем ошибка или, если такая возможность всё-таки есть, то как можно пошагово выполнять код.


Ответы (0 шт):