- Home /
Removing element from a list after it's destroyed
I'm trying to build a TD game, and in order to handle multiple enemies at once, I'm using a hashtable and a list. The logic's been working really fine, but I'm having trouble removing the enemy after it's killed.
The problem is I'm using messages to manage enemy add/removal from the list, using trigger colliders. When the enemy enters tower range, it adds to both the list and the hash and starts shooting. When it leaves, the tower removes the enemy from both the list and the map and switches target. When the enemy is destroyed, however, it doesn't trigger my "OnTriggerExit2D" function and, thus, is not removed from the list nor the map.
How are you destroying the enemy? By calling the native Destroy() method?
Answer by Hrungdak · Mar 26, 2015 at 07:42 AM
I would do the following:
Put the code to remove an enemy from list and hashtable in an extra method of the tower script, e.g. "RemoveEnemyFromList(GameObject enemy)".
call this method from OnTriggerExit2D
in the tower update function, check if all enemies in the list are still alive.
if an enemy has died, call RemoveEnemyFromList(enemy) from the Update-Function.
That's a nice idea, thank you, sir! $$anonymous$$y only question is: how bad will it affect the performance?
Performance hit should be manageable.
First its a tower defense. How much enemies can one tower have at the same time? 10? 20? No Problem. Second, its a question of architecture. If your enemy-gameobject has a method public bool IsDead(), then its a method call, not more.
For a greater amount of enemies there might be better solutions. But up to 100 enemies per tower i would do it as described.
I've tried it and, for some reason, this condition always return false: if (enemies[key] == null)
(enemies is a GameObject hashtable)
Even if the enemy has already been destroyed, any idea why?
Answer by kkxzd · Mar 26, 2015 at 12:43 AM
Really depends on how you have everything setup, but assuming you can make your list and map static members of your tower script:
public static List<int> test_list = new List<int>() {1, 2};
You can then update the value from any other script just referencing the tower script, so you could run that on the OnDestroy method:
OnDestroy()
{
TowerScript.test_list.Add(3);
}
Just modifying the list here as you didn't provide any specific code, but it should be easy to modify to fit your needs.
$$anonymous$$aybe even better it would be to have the state in an independent class, not inheriting from $$anonymous$$onoBehaviour, and you can update the property from both the Tower and the enemies. You'll have to add additional info if there is more than one Tower thou.
I dont think that this works. The static list would be the same for ALL towers in the game. As i understand this, you need an extra list for every tower.
He didn't clarify if it's one tower or multiple ones. Agree that it won't work in that case.
Sorry for not specifying that, and it's meant for multiple towers. But, using your idea, I can create a list in the enemy with the towers that are targeting it, and, upon destruction, it sends a message to all of them to remove it from their list.