//All the buildings in the game with set values of int
[System.Serializable]
public class Buildings {
public class nothing {
public static int id = 0;
}
public class tower {
public static int id = 1;
public static int health = 200;
public static string resource = "TestCube";
}
public class wall {
public static int id = 3;
public static int health = 1000;
public static string resource = "TestCube2";
}
}
Currently to access the variables I must
string getValue = Buildings.tower.resource;
which I also want.
but I want to access the value like an array like this so then I can look up all the values with resources.
All your classes use the same properties, aka id, health, resource.
So make one class
public BuildingBase {
public int Id {get;set;}
public int Health {get;set;}
public string Resource {get;set;}
public BuildingBase(int id, int health, string resource)
{
Id = id;
Health = health;
Resource = resource;
}
}
Now create a script with a dictionary:
public Dictionary allBuildings;
void Start()
{
allBuildings = new Dictionary<string, BuidlingBase>();
allBuildings.Add("nothing", new BuildingBase(0, 0, string.Empty));
allBuildings.Add("tower", new BuildingBase(1, 200, "TestCube"));
allBuildings.Add("wall", new BuildingBase(2, 1000, "TestCube"));
allBuildings.Add("gate", new BuildingBase(3, 100000, "TestCube"));
}
You can define in a script an array property of the type Building :
public class MyScript : MonoBehaviour {
public Building [] buildings;
}
You will need to initialize the array in the inspector and then you can easily access it as in the example you provided
Note that the members in the Building inner classes are static and should be changed to be non-static (otherwise they will be the same for all building instances)
Buildings is a class, so you would have to create an instance of the class. As im assuming you are wanting multiple Buildings Instances, or are you going to use Buildings as a singleton or container for buildings?
string getValueFromArray = Buildings[0].resource;
The above line of code would assume that the variable Buildings is of list type but you actually want it to be the Class. Im not sure is that is actually achievable.
I would suggest creating a container class that has the following code.
using System.Collections.Generic;
public List buildings = new List()