Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Quazifuji · Mar 29, 2015 at 09:28 PM · instantiateperformance

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiegoSLTS · Mar 29, 2015 at 11:08 PM 0
Share

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.

avatar image NoseKills · Mar 30, 2015 at 07:17 AM 0
Share

$$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.

avatar image Quazifuji · Mar 30, 2015 at 07:25 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Quazifuji · Mar 30, 2015 at 09:14 AM 0
Share

I did not know I could do that. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Instantiate vs caching GameObject in scene 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges