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?

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.