- Home /
Spawning hundred enemies on iPhone
In one level I have 120 enemies moving from the right side of the screen to the left in rows. The goal is to kill all before they reach the left side. My game is 2d and makes use of Brady's SpriteManager 2. I have up to 15 enemies on the screen at a time. I have tried following solutions for making this run smoothly:
Instantiating 10-15 enemies every time "one row" is cleared. This is not a good solution as the game freezes shortly as the enemies are being instantiated.
Instantiating all enemies at the beginning of the level, and deactivating them all until they are needed. Same problem as in solution 1. When 15 enemies is activated, the game freezes.
Instantiating all enemies at the beginning of the level, and disable the renderer on every game object until they are needed. This works okay, but it seems like it is not running completely smoothly.
Do you have a better solution or is this the way to go?
Answer by Morothar · Aug 17, 2010 at 04:52 PM
I'd go with option 3 with a small change. I'd hold them offscreen until needed instead of enabling/disabling the renderer. Also, keep in mind that you have 120 objects with the same number of scripts running. Check your Update() in the enemies script and try to optimize it. If you have unity pro you could run a copy of your project in pro and check the profiler for anything unusual.
Thank you everyone for your suggestions. I just found a thread about layer masking. In my game I have a projectile that raycasts in every update. When having so many enemies, will it make sense to set a different "layer" to the enemies off screen, as there are no need for them to be tested for collision? Thanks again.
Answer by Whimsical · Aug 17, 2010 at 04:22 PM
You can also try to replace the sprite manager with something more light-weight of your own. I have nothing against Brady's SpriteManager, it's in fact a nice tool, but it is a bit overkill for some uses, especially on iOS devices.
Isn't the sprite manager designed for use on the iPhone?
Probably, but "Built-to-fit-all-needs"-tools are usually a bit more consu$$anonymous$$g by nature. The best way to go on performance-restricted platforms like iOS is to build something of your own that really only does what you need it to do. That's usually the better way to go.
Answer by Wolfram · Aug 17, 2010 at 04:23 PM
Try optimizing the causes, not the symptoms: How complex is a single enemy? Especially draw calls, maybe also number of sub-objects, scripts, components, etc. Reduce it to the bare minimum.
Answer by JordanCannaday · Jul 22, 2011 at 01:50 PM
Iphone has many enemies including all of the androids and droids blackberrys and sprints palm pre and more
Your answer
Follow this Question
Related Questions
Can't seem to destroy instantiated objects. 1 Answer
Fruit Ninja - Cut fruit effect 3 Answers
Finding children of an instantiated object 1 Answer
Accessing prefab instances at runtime (unity iphone) 2 Answers
Have a spawning issue on iOS 0 Answers