- Home /
OnTriggerEnter - destroy (this.gameobject) if it collides with anything
Hi everyone
Quick question and I have a feeling its a quick answer as I think I'm just being dumb. With the method OnTriggerEnter I my gameobject to destroy itself if it collides with anything physically in the game world, whether it be an enemy or world geometry.
Any help is greatly appreciated and if it helps get me an answer then here some basic sudo code.
void OnTriggerEnter (Collider collider)
{
if (this.gameObject collides with anything in the world)
{
destroy (this.gameobject);
}
}
Answer by paulygons · Nov 25, 2013 at 01:55 AM
Wait a second, isn't the OnTriggerEnter already telling you that your object has hit something? So you could go straight to destroy, right?
void OnTriggerEnter (Collider collider)
{
destroy (this.gameobject);
}
Answer by DavidMilot · Nov 25, 2013 at 04:41 AM
(Not sure if my last post was properly posted cause my account is newly registered 10 minutes ago)
But did you forget to add a component called Box Collider or any other Collider to your gameobject?
The object in question is a particle emitter which is used as a players weapon. The above code works, and I knew it would be a simple fix :) but in the hierarchy clones of the particle emitter still exist, any ideas?? Sorry for slowness of reply I have been busy with work.