Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 endasil_unity · Feb 02 at 04:17 PM · destroydestroy object

Spawned objects randomly not destroyed

I'm having some serious problem figuring out why red and blue balls are not destroyed in the same amount when I spawn them in a close space.

Here is a simulation where i spawn 100 red and 100 blue balls. https://www.youtube.com/watch?v=0Khs9rVCBMg

Blue balls have a script like this, where they just update the destruction counter by 1 upon destruction

 public class BlueBall : MonoBehaviour
 {
     private SpawnManager spawnManager;
     private void Awake() => spawnManager = FindObjectOfType<SpawnManager>();
     private void OnDestroy() => spawnManager.AddBlueDestroyed();
 }

Red balls have a script like this, where they will destroy themselves and a blue ball upon collision

 private void OnCollisionEnter(Collision collision)
     {             
         if (collision.gameObject.CompareTag("Blue") && dying == false)
         {                     
             dying = true;
             Destroy(collision.gameObject);
             Destroy(gameObject);
             spawnManager.AddDestroyCount();
         }     
     }

I'm setting dying to true here to prevent a red ball from destroying multiple blue balls (as actual destruction happens at the end of frame not immediately). The problem i am having is that much more red balls are destroyed than blue balls, even though there is only one place of destruction and it destroys both. As blue balls are destroyed before the blue balls in the code, we know that there is no error in this code before that, which would prevent the code from getting to the point it destroys the blue balls.

This mismatch only happens when I spawn the balls close together. If I change the scale of the room I spawn then from 5x5 to 50x50 the number of balls destroyed will be equal. What is causing this?

public git repo with the project can be found at https://github.com/endasil/DestructionProblem if someone wants to look at the exact details. Would really appreciate it if someone could help me figure out the exact mechanics of what is happening here because I spent hours trying to understand how this works.

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 jmgek · Feb 02 at 04:37 PM 0
Share

One of the things you should do is look into pooling, as destroy isn't the best way to handle your logic of this app.

avatar image endasil_unity jmgek · Feb 02 at 05:10 PM 0
Share

This is not a question about performance optimization. The purpose of this experiment is to understand the exact mechanics behind object destruction, what causes an object to be destroyed and not.

avatar image endasil_unity · Feb 02 at 06:33 PM 0
Share

Thank you very much, that solved my issue!

1 Reply

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

Answer by jmgek · Feb 02 at 05:26 PM

Yes I understand this, but what I'm saying is you should focus on best practices. Destroy should rarely be used in a production application, the GC cost alone is not worth it and also can create dangling reference issues that are difficult to catch.

Your "experiment" serves little purpose and your validation to figure out what the mechanics behind the destruction makes little sense as this isn't a good test.

But if you want me to answer your question, the reason is that a blue collider can trigger multiple balls with one collider. So 2 or more red balls can be triggered by the same blue ball on a physics frame. How you would get around that is by putting a condition to only trigger a 1 to 1 ratio.

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 endasil_unity · Feb 02 at 06:38 PM 0
Share

Thank you for providing the solution to the problem! Please post your solution as an answer so I can give you credit for fimding the solution.

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

138 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Enemy Spawner help 1 Answer

Why would a GameObject delete itself. 0 Answers

Clear reference when destroying gameobject 4 Answers

transferring variables between scripts c# 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