I am making a game, and I am trying to make my game moddable, so I am making that the game reads a list of possible buildings and their stats line by line, from a text file, and then splits the values accordingly. Everything seems to be working fine, I am even using a pretty similar piece of code in another part of the game and it works just fine, but when I try to use it here, it gives me this exception.
public static void parseBuildings()
{
Building[] parsedBuildings = new Building[500];
string[] buildingList = System.IO.File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\data\buildings.turd");
int i = 0;
foreach (string line in buildingList)
{
string[] parts = line.Split(',');
//MessageBox.Show(parts[0] + parts[1] + parts[2] + parts[3] + parts[4] + parts[5]);
//File format - name,description,unused(price),baseprice,count(unused),hps
parsedBuildings[i].name = parts[0];
parsedBuildings[i].description = parts[1];
//parsedBuildings[i].price = int.Parse(parts[2]);
parsedBuildings[i].baseprice = int.Parse(parts[3]);
//parsedBuildings[i].count = int.Parse(parts[4]);
parsedBuildings[i].hps = int.Parse(parts[5]);
i++;
}
Array.Resize(ref parsedBuildings, buildingList.Length);
buildings = parsedBuildings;
}
This here makes the game fill the array 'buildings' from a temporary array called 'parsedBuildings'
The funny thing is that the NullReferenceException happens at the parsedBuildings[i].description = parts[1]; line, and the commented message box works just fine.
This is how the 'buildings' array is instantiated.
public static Building[] buildings = {new Building("Loading", "Loading", 1,1,0,0)};
And this is what the Building class looks like
public class Building
{
public string name;
public string description;
public int price;
public int baseprice;
public int count;
public int hps;
public Building(string nam, string desc, int pri, int bpri, int cnt, int hp)
{
name = nam;
description = desc;
price = pri;
baseprice = bpri;
count = cnt;
hps = hp;
}
}
Please help me get this solution working, because it is miles better than what I previously used.