Ваш Tile
относится к типу MonoBehaviour
! MonoBehaviours
нельзя создать как обычные классы, но только путем добавления их с помощью AddComponent
к определенному GameObject
; они не могут существовать без привязки к GameObject
.
Поэтому ваша линия
public Tile[,] map = new Tile[40, 40];
на самом деле не создает заполненный массив с экземплярами со значениями по умолчанию, но вместо этого создает двумерный массив, заполненный null
заполнителями для Tile
ссылок , а не экземпляров.
Я думаю, что вы хотите вместо этого:
плитка
// use this tag to make it
// serializable and e.g. visiable in the inspector
[System.Serializable]
public class Tile
{
// use fields instead of properties
// since also properties are not serialzed
public int ceiling;
public int northWall;
public int westWall;
public int floor;
}
и
public class Dungeon : MonoBehaviour
{
public Tile[,] map = new Tile[40, 40];
private void Start()
{
for (int y = 0; y < 40; y++)
{
for (int x = 0; x < 40; x++)
{
// For simple types like e.g. int, string etc there are default values
// so the array would be filled with those values.
// However your class Tile has the default value null
// So you have to create Tile instances using the default constructor like
map[x, y] = new Tile();
// And than optionally give it values
// though they will be 0 by default of course
map[x, y].ceiling = 0;
map[x, y].northWall = 0;
map[x, y].westWall = 0;
map[x, y].floor = 0;
// or alternatively
map[x, y] = new Tile()
{
ceiling = 0,
northWall = 0,
westWall = 0,
floor = 0
};
}
}
}
}
Но лично я всегда предпочел бы иметь правильный конструктор и имена полей (public => PascalCase)
[System.Serializable]
public class Tile
{
public int Ceiling ;
public int NorthWall;
public int WestWall;
public int Floor;
public Tile(int ceiling, int northWall, int westWall, int floor)
{
Ceiling = ceiling;
NorthWall = northWall;
WestWall = westWall;
Floor = floor;
}
}
и с использованием
public class Dungeon : MonoBehaviour
{
public Tile[,] map = new Tile[40, 40];
private void Start()
{
for (int y = 0; y < 40; y++)
{
for (int x = 0; x < 40; x++)
{
Tile[x, y] = new Tile(0,0,0,0);
}
}
}
}