Prevent instantiating on collider

Hello!


I make a game where planets generate automaticly. There is a problem that sometimes they generate touching an other one.
How can I destroy the previous planet if a new one instantiate on it? Like removing the old for the new planet. Thanks!


I use a prefab for the planet

1 Answer

1

You could try something like this:

   // in your "planet" script
    
    var dateOfBirth : float;
    
    function Start() {
       dateOfBirth = Time.timeSinceLevelLoad;
    }
    
    function OnCollisionEnter(other) {
       otherPlanet = other.gameObject.GetComponent("planet");
       if (otherPlanet && otherPlanet.dateOfBirth < dateOfBirth) {
          Destroy(other.gameObject);
       }
    }

It doesn't work :( The planet stays "inside" the previous one...

They both have the script attached, right?

Of course. The instantiated planet is a "clone" of the original

I guess debug to make sure OnCollisionEnter gets called at all (i'm assuming they have colliders), then debug to make sure GetComponent is returning something, then debug to make sure their dates of birth are different. Maybe if it's a complete clone, their dates of birth are equal?

Yes, it was that the dates of births are equal! thanks!