- Home /
[SOLVED] How to Destroy a rigidbody game object on collision with another gameobject?
PS: Answered in best answer below! check it out! :) feel free to PM me on discord: Khaled#1130 if you have any questions, I might just be able to help!
Hello!
I am sure this is answered some were but I have searched for too long.. still am and still suck at solving the problem. I am an utter noob but oh well, did not find an answer to my specific case so i am hoping one of you loves would be able to help me with it.
My project is in 2D.. a rigid body 2D ball that moves by forces controlled by the player would not get destroyed in contact with another object (laser) The ball has a circle collider 2D, a rigidbody2D and a script that controls movement and forces The laser has a boxcollider 2D and a script that controls the death of the player
The script on the laser is (other is refered the Player)
using UnityEngine;
using System.Collections;
public class DestroyPlayer : MonoBehaviour
{
public GameObject other;
void OnTriggerEnter2D(Collider2D other)
{
Destroy(other);
}
}
I have tried numerous other scripts and kept trying different edits and it just would not work. Thank you!
Answer by $$anonymous$$ · Jul 04, 2017 at 07:02 PM
Hello @RadiationAlert and @altaiirdesmond and @RhisisLS
First iam extremely sorry I could not reply earlier because my router was really crappy. ..... ........
I tried your method @altaiirdesmond but sadly it did not work and only made a compiler error, I belive it was because its gameObject instead of gameobject, anyway I happened to have a friend over discord who happened to know alot about these kind of stuff, all he did really was view some documentations and voila! he gave me the code that actually worked!
.......
You attach this to the player not the laser object, also create a tag for your Player called "Player" (cap sensitive) and asign it to your player.
There is the script
using UnityEngine;
using System.Collections;
public class DestroyPlayer : MonoBehaviour {
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Player") //Change tag
Destroy(coll.gameObject);
}
}
My concern however is why did not the other scripts work? They all practicely say the same thing.
PS: (the name (DestroyPlayer) in the script above will depend on what you name your script by, do not copy and paste this unless you have named your script DestroyPlayer exactly (cap sensitve) or edit it to your script's name. Other wise you will recive an error but this is easily fixed.
Answer by RadiationAlert · Jun 20, 2017 at 03:07 AM
@Khaled125 Well from the looks of it, your colliders might not be set to triggers? Otherwise, altaiirdesmond's solution should work correctly.
Answer by altaiirdesmond · Jun 20, 2017 at 12:27 AM
create a tag "Laser" then set the tag of the laser gameobject to Laser . Attach this script to the player
public class DestroyPlayer : MonoBehaviour
{
//I have removed the "public gameobject other" since I'm assuming that this script is
//attached to the player.
void OnTriggerEnter2D(Collider2D other)
{
//if the player collider hits a gameobject with tag "Laser"
//then the gameobject the script is attached to will be destroyed
if(other.gameobject.tag == "Laser")
Destroy(other);
}
}
Answer by Hamilcar-games · Jul 04, 2017 at 09:26 PM
void onCollisionEnter() { destroy(gameobject); }