- Home /
GameObject disappears when enabling Trigger
I have a Parent GameObject which I added a collider, and his grandchild has also a collider, when I enable the collider trigger of his grandchild, the parent GameObject disappears? Is this a bug? if not how would you go and fix it, so it does not disappear.
Is the parent object set to die/destroy itself on a collision? If so, are these two colliders in contact?
You're using a script somewhere I'm sure. Post the script please.
You may be right, the grandchild does have this script. I want the asteroid to destroy and not the parent planet. I need to find out why the planet is being destroyed.
using UnityEngine;
using System.Collections;
public class CollisionDetecter : $$anonymous$$onoBehaviour {
public GameObject explosion;
void OnTriggerEnter(Collider asteroid) {
var spawnSpot = asteroid.transform.position;
Destroy(asteroid.gameObject,0.01f);
GameObject newObj = Instantiate(explosion, spawnSpot, Quaternion.identity) as GameObject;
newObj.transform.parent=transform;
newObj.tag = "Clone";
}
}
Answer by d112570 · Nov 14, 2013 at 12:27 AM
I told it to avoid its grandparent. Here's the new code.
void OnTriggerEnter(Collider asteroid) {
if (asteroid.name != transform.parent.parent.name) {
var spawnSpot = asteroid.transform.position;
Destroy(asteroid.gameObject,0.01f);
GameObject newObj = Instantiate(explosion, spawnSpot, Quaternion.identity) as GameObject;
newObj.transform.parent=transform;
newObj.tag = "Clone";
}
}
Had it tested and it works. Asteroids can hit planet, and Asteroid destroyed and debris appear.
Nice, that makes sense. Although, that means that the OnTriggerEnter method is getting called repeatedly because it's detecting a collision and that's probably not performance friendly. Think about it happening when you have multiple instances of this going on.
It only gets called when I visit this planet and there is a Asteroid Script attached.
Your answer
Follow this Question
Related Questions
When collider enters trigger, colliders parent = triggers parent 1 Answer
Can't click gameobject when over another trigger? 1 Answer
How to prevent a game object from colliding with its parent 2 Answers
Parent Script accessing Child components 1 Answer
Trigger BoxCollider Fails to Move With GameObject After Resize 0 Answers