Collision Detection Help!
hello, I am making a mini game and was just wondering if anyone could possibly help me with some collision detection. Currently I have an item with Rigidbody2D and a BoxCollider2D attached. It falls from the sky. I then have a floor(With a BoxCollider2D attached aswell) and when the object hits the floor, I want it to get destroyed. I am currently trying:
void Start()
{
gameScore = 0;
setScore();
}
void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.tag=="floor")
{
gameScore++;
setScore();
Destroy(col.gameObject);
}
}
void setScore()
{
scoreText.text = "Score: " + gameScore.ToString();
}
The falling objects get spawned in with instantiate()
Currently I'm getting no response from this. If anyone has any idea how this could help me, I would really appreciate it. Thanks for your time!
Answer by Zoogyburger · Feb 19, 2016 at 06:24 PM
You set coll to be the floor and told it to be destroyed! Instead of
Destroy(col.gameObject);
put:
Destroy(gameObject);
Your answer
![](https://koobas.hobune.stream/wayback/20220612064840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Checking for collisions 1 Answer
Raycast does not seem to properly detect object it hit 0 Answers
Player trigger on platform with prefab 0 Answers
Destroy GameObject inconsistent with Instantiated children 1 Answer
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers