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 /
  • Help Room /
avatar image
0
Question by Ergon · Feb 23, 2016 at 11:13 PM · gameobjectdestroydelayontriggerstay

Destroy(GameObject) works after 3 attempts

Hello, this is my first question in unity community, so please be kind :)

I have a game which has a planet and many asteroids. I ve made a very long script, so I won't share all of it. I hope this will be enough.

I ve created colliders for both objects and my goal was to calculate if more than half of asteroid is in the planet (collider). If it is true, then the asteroid should be destroyed. And I did it with this very same code. I wrote this code a few days ago and it was working fine until yesterday. I did many changes in my game and these changes have nothing to do with these lines of codes and objects. Mostly GUI changes and other stuff I ve not implemented yet...

The problem is, somehow "if" statement started run 3 times always. I used debugger and saw that it runs every single line of code 3 times in the "if" statement. This wasn't happening before. Here is the code:

 void OnTriggerStay(Collider otherObj)
     {
         if(otherObj.gameObject.tag == "Asteroid") // Swallow asteroid
         {
             Swallow(otherObj);
         }
     }
 
     void Swallow(Collider otherObj) // Swallow ( Asteroid )
     {
         // Calculating if the half of object which will be swallowed is in the collider
         Vector3 distanceVector = otherObj.transform.position - gameObject.transform.position;
         float distance = Mathf.Sqrt(Mathf.Pow(distanceVector.x, 2f) +
                                     Mathf.Pow(distanceVector.y, 2f) +
                                     Mathf.Pow(distanceVector.z, 2f));
 
         if (distance < (radius / 2))
         {
             gameObject.GetComponent<Rigidbody>().mass += otherObj.GetComponent<Rigidbody>().mass; // Add object's mass to planet
             Destroy(otherObj.gameObject);  // Destroy Object
             AsteroidManager.asteroidAmount--; // Decrease asteroid amount on map
             volume = gameObject.GetComponent<Rigidbody>().mass; // Set volume again
             radius = Mathf.Pow(volume / 4, 1f / 3); // Set radius again
             rotationAngle = new Vector3(0f, 1.6f, 0.4f) * ((maxMass - volume) / maxMass); // Set rotation angle of planet again
             massText.text = "Mass: " + volume;
         }
     }



"if" statement should be called only once since the object must be destroyed after first run. Also, I have to say again that the weird part is ,It always runs these lines 3 times, not 2, not 4. After 3 runs, it destroys the object. It works like it has delay or something. I had similar problem before, because I forgot to delete the delay for Destroy, but now it doesn't have delay. So, I m hoping that someone can explain the problem.

P.S: as much as i know, OnTriggerStay runs one time every frame and destroy object runs at the end of frame, but this shouldn't cause a problem. Even if it does, it shouldn't run 3 times, right? Also, I tried using coroutines to create a gap between Destroy object and the lines after coming it, but it ran 3 times again including coroutines this time...

Comment
Add comment · Show 2
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 bubzy · Feb 24, 2016 at 12:01 AM 0
Share

have you tried OnTriggerEnter() ?

avatar image Ergon bubzy · Feb 24, 2016 at 12:28 AM 0
Share

As i know, OnTriggerEnter is triggered only once when colliders collide for the first time. So, I need to use OnTriggerStay to calculate the distance between two objects over and over again until "if" statement becomes true.

0 Replies

· Add your reply
  • Sort: 

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

53 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

Related Questions

GameObject.Destroy(gameObject); Not Working? 0 Answers

When instantiating 3 clones are created rather then 1 2 Answers

Static Gameobjects Destroy on Reload 0 Answers

Gameobject is null for no reason 1 Answer

The bullet never destroyed!!! 2 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