- Home /
How to make enemies stop spawning
So I have an infinite zombie spawning game, much like Call of Duty: Zombies, and I have 8 GameObjects around the map that act as spawners spawning 1 enemy every 11 seconds, or eight enemies every 11 seconds if you count all the spawners, but the problem is, within a minute or so, there are so many enemies that the game lags then crashes, so how can I make enemies stop spawning after so many are spawned then start spawning again when that number is reduced by the player killing them, and how many enemies do you recommend exist at once? Thanks in advance!
Answer by Effervescence · Aug 21, 2016 at 05:51 AM
@kadenatmore Are you using object pooling for these enemies? If you are, then it's easy enough to limit the enemies by limiting how many you create in the first place. Object pooling is basically creating a list of the gameobjects you want to pool, and setting them active/inactive as needed. Link to tutorial in case you haven't seen it yet: https://unity3d.com/learn/tutorials/topics/scripting/object-pooling .
If you're not using object pooling (although I would recommend doing so), you could technically keep a counter of how many enemies are on-screen, stop spawning when they reach a certain point, and restart after they fall below another point, but...that's probably not a good way to go about it ^^'
Finally, as for how many enemies should exist at once...honestly it depends on how much they're doing (do they actually go after the player and shoot bullets at them or do they just stand there and kill the player on contact?). Best way to find out is just to test different numbers, I'd say. ^^'
Yeah I'm not using pooling, I don't think, I didn't write the spawn code myself, but I'm 99 percent sure it uses Instantiate, and they just run into the player and do 10 damage every second they are in contact, is object pooling god for spawning enemies infinitely (But only so many at once)?
Yeah, object pooling is definitely very good for things like enemy spawning, bullets, etc. Instantiate() and Destroy() are a bit more taxing on your computer.
Sorry for this, but I'm horrible at scripting, could you provide with a script or link ti script please if possible, thanks!
Your answer
Follow this Question
Related Questions
Script Crashing Unity 1 Answer
What should I do to prevent crashing? 0 Answers
How can I spawn without glitch 1 Answer
Script causing lag 1 Answer
Why does unity crash leads to direct3D and hardware acceleration disappear ? 2 Answers