- Home /
Problems instantiating multiple objects at once, fine when instantiating them quickly in succession
EDIT: Solved the Problem. Because the bullets spawned on top of each other, they all triggered each other's OnTriggerEnter functions. From earlier testing, the OnTriggerEnter function of the bullet had two Debug.Log statements at the beginning. So every time I fired a shotgun with n bullets, it would have to do 2n^2 Debug.Log calls, and apparently doing 30+ simultaneous Debug.Logs makes Unity unhappy.
Original Post:
I'm working on an isometric shooter game (based on the Survival Shooter tutorial) with different weapon pickups that can be dropped by enemies. Right now, the pickups can be shotguns or machine guns. Machine guns shoot rapidly (right now up to 33 times per second), shot guns shoot slowly (up to 4 times per second) but shoot a spread of multiple bullets with each shot.
When the gun shoots, the code instantiates a single bullet for a machine gun, multiple bullets (4-12) for a shotgun. With the machine gun, I have no problems. Even for an extremely rapid-firing machine gun that results in dozens of bullets on screen at once, the game keeps running just fine.
With a shotgun, the game stutters every time I shoot. It's a very brief (but noticeable) stutter for a 4-bullet shotgun, but quite long for a 12-bullet shotgun.
There are other places in my code where multiple objects get instantiated at once without a problem. In particular, certain common scenarios involve four enemies being instantiated at the same time (and the enemies are much more complex, both graphically and in terms of code, than the bullets). But instantiating four bullets at once messes things up.
Why does simultaneously instantiating multiple bullets cause problems when rapidly instantiating them (but not simultaneously) doesn't? And more importantly, how can I fix this so I can include shotguns in my game?
Can you share some more info on how bullets objects are composed and the scripts they use? Also, the code that instantiates the bullets could be usefull.
I can only suggest you create a new prefab of an empty game object, and instantiate that ins$$anonymous$$d of the actual bullet. Then start copying components from the bullet prefab onto the empty prefab one by one, to test that's causing the slowdown.
If it's still slow with an empty prefab then the instantiation itself is the problem.
$$anonymous$$y first guess is that the shotgun bullets (with colliders) are instantiated on top of each other so they cause a large amount of collisions for the first few frames after instantiation.
$$anonymous$$aybe you should disable bullet-to-bullet collisions if they are enabled now. Also you should pool the bullets so you instantiate them in the beginning of the game and recycle them without calling Instantiate later.
Okay, I took your approach, adding one component to an empty bullet at a time (and eventually commenting out different sections of code to essentially remove parts of components), and found the problem.
Because the bullets spawned on top of each other, they all triggered each other's OnTriggerEnter functions. From earlier testing, the OnTriggerEnter function of the bullet had two Debug.Log statements at the beginning. So every time I fired a shotgun with n bullets, it would have to do 2n^2 Debug.Log calls, and apparently doing 30+ simultaneous Debug.Logs makes Unity unhappy.
Thank you very much.
Answer by Socrates · Mar 30, 2015 at 08:05 AM
The answer to your Edit issue is that you put the bullets all on the same layer, then choose in Project Settings that items on that layer do not collide with other objects on that layer. If you do this, your bullets will never call the OnTriggerEnter() and won't cause the Debug.Log() overload.
Your answer
Follow this Question
Related Questions
GameObject-Pool to avoid performance issues in Instantiating many objects? 3 Answers
performance question about particles and instantiating 1 Answer
Performance issues when creating many tiny objects 1 Answer
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer