I'm using a static class as some kind of library (maybe not the best idea ever but I don't really know what I'm doing...) and I have some fields with objects so I can access them easily by name. But I still need to have an array that I can use a method to search through.
But when I call that method GetTypeByName("Iron Mine") it should return look through the array and return the ironMine object, but instead it just screams "Object reference not set to an instance of an object"... I'm clearly missing something about static arrays or arrays in general, I'm not that used to them yet...
public static class IndustryTypes
{
public static IndustryType[] allTypes = //This is that array that seems to mess things up...
{
ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine,
quarry, oilWell,
farm
};
public static IndustryType noType;
public static IndustryType ironMine, coalMine, aluminiumMine, copperMine, uraniumMine, goldMine;
public static IndustryType quarry, oilWell;
public static IndustryType farm;
static IndustryTypes()
{
noType = new IndustryType("noType", 0, Color.Red);
ironMine = new IndustryType("Iron Mine", 50000, Game.color_mine);
coalMine = new IndustryType("Coal Mine", 40000, Game.color_mine);
aluminiumMine = new IndustryType("Aluminium Mine", 100000, Game.color_mine);
copperMine = new IndustryType("Copper Mine", 55000, Game.color_mine);
uraniumMine = new IndustryType("Uranium Mine", 150000, Game.color_mine);
goldMine = new IndustryType("Gold Mine", 125000, Game.color_mine);
quarry = new IndustryType("Quarry", 25000, Game.color_mine);
oilWell = new IndustryType("Oil Well", 110000, Game.color_oil);
farm = new IndustryType("Farm", 10000, Game.color_farm);
}
public static IndustryType GetTypeByName(string name)
{
for (int i = 0; i < allTypes.Length; i++)
{
if (name == allTypes[i].name) //This says "Object reference not set to an instance of an object" or something like that...
{
return allTypes[i];
}
}
return noType;
}
}
This is all in a static class called "IndustryTypes", so I don't need to instantiate it.
The class "IndustryType" is a non-static class.
Ask if you don't understand what I mean... I don't really know myself but I'll try! :D
Here's the "IndustryType"-class:
public class IndustryType
{
public string name;
public int cost;
public Color color;
public List<Resource> components;
public List<Resource> products;
public IndustryType(string _name, int _cost, Color _color)
{
name = _name;
cost = _cost;
color = _color;
}
}
Thanks a lot for taking your time!