- Home /
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!
use a prefab for the planet*
Answer by armoredpokey · Apr 23, 2013 at 05:22 PM
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...
Of course. The instantiated planet is a "clone" of the original
I guess debug to make sure OnCollisionEnter gets called at all (i'm assu$$anonymous$$g they have colliders), then debug to make sure GetComponent is returning something, then debug to make sure their dates of birth are different. $$anonymous$$aybe if it's a complete clone, their dates of birth are equal?
Yes, it was that the dates of births are equal! thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613100856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to instantiate on collision? 0 Answers
Instantiated Prefab's script doesn't work 2 Answers
Destroy a GameObject 2 Answers