I’ve got a list of gameObjects and I want to track the unique objects in the list as well as the count of each unique object. I’m trying to accomplish this by using LINQ and doing two different things: first, return the unique gameObjects as gameObjects and second I need to keep track of the count of each type of gameObject.
More specifically, I’ve got a list of troops and there are many different types of troops. In the UI I want to keep track of the type of troop and the current number of them on the battlefield like so:
For the image above the gameObject list would be 100 gameObjects where 25 are soldiers, 25 are archers, and 50 are cavalry troops.
My idea is to return the unique gameObjects from that list using .Distinct()
so I know how many different types of troops there are and can .GetComponent
and grab their sprites. Then I need to count how many of each unique game object there are for the troop totals.
Every time a troop is added or removed from the list, a function in the UI manager gets called to update the totals. I’m trying to use LINQ queries and I just can’t figure out how to return a gameObject when I use .GroupBy()
.
// BattleManager.cs (singleton)
// Gets updated every time a player troop is spawned or despawned
public List<GameObject> playerTroops;
// UITroopCounter.cs
public GameObject troopCountPrefab;
private int _uniqueTroopCount = 0;
public void UpdateTroopCounts()
{
var _groupedTroops = BattleManager.Instance.playerTroops.Select(g => g.gameObject).GroupBy(g => g.name).ToList();
if (_uniqueTroopCount != _groupedTroops.Count())
{
//Clean slate
foreach (Transform child in transform)
Destroy(child.gameObject);
//Create a troopCounter object for each unique type of troop
foreach (var troop in _groupedTroops)
{
var _newTroopCounter = Instantiate(troopCountPrefab, transform).GetComponent<UITroopCount>();
_newTroopCounter.icon = troop.GetComponent<TroopCore>().icon;
_newTroopCounter.counter = troop.Count();
}
}
//Once these objects are created, I need some way to keep them attached to the count of the type of troop they are
//Troops can be revived and new types of troops can be summoned during the battle as well
}
}
Even though I’m doing .Select(g => g.gameObject)
it comes back in a format I can’t use .GetComponent
on. Seriously, if there’s a better way please let me know because this is feeling pretty squirrely at this point. Thanks!