Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Oscar-Bear · Apr 25, 2017 at 12:09 AM · c#2dphysics2doncollisionentermobile devices

Is calling OnCollisionEnter2D on allot of GameObjects bad for performance?

Hi!

I want to make a 2D game for mobile devices using C#. The goal of the game is to shoot objects with a bullet. In my scene I have always only one bullet and around 70 targets.

Is it better for performance to have ONE Gameobject (the bullet) with a script having the function: OnCollisionEnter2D, or is it okay to have around 70 Gameobjects that all have a script with the OnCollisionEnter2D function?

I ask this because if the OnCollisionEnter2D is called every frame like Update (not sure about this), its probably better (for performance) to call the function only on one Gameobject, rather than on 70?

Thanks in Advance!

Note: I'm just starting out with Unity. This is also my first question on the forum. I'm sorry if i ask this in the wrong place or if my question is not clear enough.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Commoble · Apr 25, 2017 at 05:42 AM

OnCollisionEnter2D is only called in the single frame in which another object begins to touch your object with the script (as opposed to OnCollisionStay2D, which is called for every frame while another object is touching your scripted object).

Furthermore, it doesn't matter if you have one object with the script on it or seventy; collision event functions will only be called on the specific objects that are colliding. Having a lot of physics-enabled objects can impact performance if you have a lot of them, but this performance impact will be the same with or without having OnCollisionEnter2D in those object's scripts.

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 Oscar-Bear · Apr 25, 2017 at 12:36 PM 0
Share

Thank you for your clear explanation. You're answer helped me allot!

avatar image
0

Answer by Arminyazdian · Apr 25, 2017 at 01:17 PM

No. it just called one time when a game object collides with it. But for detecting a collision or a trigger between two objects you are suppose to add a rigid body component to one or the both. this would decrease the performance a bit.

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking for collision with 2D objects/sprites 3 Answers

How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers

Move Physics2D Object With Mouse Using AddForce? 0 Answers

Setting my position to the position of a gameobject 0 Answers

Question about Scripting a slingshot mechanic 0 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