- Home /
Tower defense target update
Hey. I'm making a tower defense game, which is the first game I have ever made with Unity. Everything was working smoothly until I decided to pool my enemies and bullets (both are prefabs), at which point I ran into an issue I can't seem to figure out how to fix..
The problem I'm having is that when my towers destroy enemies, they get disabled and thus returned to the pool as they should, but then it usually happens that they get instantly re-enabled and spawn at the starting location so soon that my turrets haven't updated their target yet, and they overshoot their range to the re-enabled enemy. I have a method that gets called every frame. It finds the closest enemy to set it as target.
I have tried putting all kinds of checks into my code to hack my way out of this issue, but can't find a way. Any help would be appreciated.
the second the enemy gets destroyed and goes back into it's pool, the turrets target should be nulled. this way, next frame checking for the closest one it will have to pick a different one.
I tried this. I go through all the turrets in the scene and check if their target is the enemy to be returned into pool and if so - set their target to null. But still, the same problem persists.
I guess before pooling you destroyed and then instantiated objects. There must be a difference to your current approach, where the most obvious would be auto-null on destroy, while manually handling it now. You should carefully compare the difference.