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...
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.
Your answer
Follow this Question
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