I’m still very new to Game Development and Unity, and I’m wondering if a scenario I’ve run into is a valid time to cache out of convenience, or if there’s a better way to accomplish the same thing and avoid caching.
I’m making a simple tower defense game, and a tower needs to choose an attacker to target, until that target is gone. The tower needs to know which attacker it is targeting, and I can only think of two ways to do this.
-
Cache the closest attacker, and check that the target still exists each loop. This way, I only need to search through all of the attackers and find the closest on occasion. However, this does cache a GameObject that I know will eventually be Destroyed.
-
Re-find the closest attacker each loop. This requires searching through all of the attackers each time for each tower (which will become more time consuming with more attackers and towers). This will also not have the same behavior, because it may find a new closest attacker before the previous target has been destroyed.
Neither of these seem like the correct solution to me, but I can’t think of another (relatively) simple way to accomplish this task without caching.