ok, so after testing I noticed that if the Unit gets destroyed, “OnTriggerExit” does not trigger, and it leaves a null in my list of units. (how do I go about purging that null from my list(HashSet).
Furthermore, how do I remove that destroyed object without iterating though the list (if at all possible).
Couldn’t you make it so that when your Unit gets destroyed, it first removes itself (however it’s implemented, using event or otherwise) and after that you destroy it? If you are using a HashSet like you said, you don’t have to iterate it.
If removing from the HashSet before the unit is destroyed was an Option, I would have done that. but this GameObject could be in 100 different Hashsets, And I wouldn’t be keeping track on which Hashset its in.
Can you elaborate a bit more how you got into that situation? Sounds a bit complicated to me. But even so, if you got your item (Unit) in thousands of HashSets and you wanted to get it removed, you could trigger an event and then all those thousands of whatevers that have the HashSets are listening to that event. Then, they all remove that Unit from their HashSet:
if (units.Contains(toBeDeleted)) {
units.Remove(toBeDeleted);
}
After that, the Unit self destructs, and as the HashSets have already removed that unit, there’s no nulls left or such.
Thanks bro, I will look into that, and see if I can make it work. Someone Just told me something about “delegates”… no idea what that is but the way he explained it sounds like what you suggested.