My current setup is that I have 2000+ empty gameobjects in a scene all called “Hex” with a tag “Hex” and with a script attached called “Hex” that stores all the relative information about each hex.
like:
- movementCost
- adjacentHexs
- connectedHexs “for linked non-adjacent hexs”
- weightValue “for each player”
- ect… ect…
the problem is that I am using code like this alot, over and over again to do different things like move players, enemies or alter the map:
foreach (GameObject hex in GameObject.FindGameObjectsWithTag("Hex"))
{
int Weight = hex.GetComponent<Hex>().weight;
//do something
int movementCost = hex.GetComponent<Hex>().movementCost;
//do something
GameObject[] adjacentHexs = hex.GetComponent<Hex>().adjacentHexs;
//do something
foreach (GameObject adjacentHex in adjacentHexs)
{
int adjacentWeight = adjacentHex.GetComponent<Hex>().weight;
//do something
int adjacentMovementCost = adjacentHex.GetComponent<Hex>().movementCost;
}
//do something
}
I am using the GetComponent over and over again all over my code and its getting ridiculous. I need a better way of working that will aloow me to store all the data I need about all the Hexs in one place so that I can access it without the repeated GetComponent.
I was looking into creating a Hex class and then creating a list but Im not sure how to implement it or how to make it work without the GetComponent slowing everything down.
Any ideas?