Есть класс WorldGenerator со вложеным циклом. Ошибка возникает на строке
GameObject block = Instantiate(DB.blocks[0].gObj, new Vector3(x, y, 1), Quaternion.identity);
Полный текст ошибки: NullReferenceException: Object reference not set to an instance of an object WorldGanerator.Generate (System.Int32 size) (at Assets/Scripts/WorldGanerator.cs:22)"
public class WorldGanerator : MonoBehaviour
{
public GameObject PlayerInventoryPanel;
public GameObject Player;
DataBase DB;
private void Start()
{
DB = gameObject.AddComponent<DataBase>();
}
public void Generate(int size)
{
for(int y = -size/2; y < size; y++)
{
for (int x = -size/2; x < size; x++)
{
GameObject block = Instantiate(DB.blocks[0].gObj, new Vector3(x, y, 1), Quaternion.identity);
block.GetComponent<SpriteRenderer>().sprite = DB.blocks[0].Img;
}
}
Instantiate(Player, Vector3.zero, Quaternion.identity);
}
}
Функция Generate вызывается из класса Game в Start
public class Game : MonoBehaviour
{
DataBase DB;
WorldGanerator WG;
public GameObject PlayerInventoryPanel;
private void Start()
{
DB = gameObject.AddComponent<DataBase>();
WG = gameObject.AddComponent<WorldGanerator>();
DB.Setup();
WG.Generate(10);
PlayerInventoryPanel.SetActive(false);
}
}
В ошибке вроде написано что проблема в переменной size, но я не могу понять в чём именно