Reputation: 87
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Bullet")
{
Destroy(gameObject);
Debug.Log("hit");
}
}
This is my code, I appreciate that there is a lot of other sources out there however I have looked on the internet and just cannot understand where im going wrong. I know its probably something so small. I'm attempting to get on collision detect destroy "this" game object using the tag. The Bullet prefab has the Bullet tag and is spelt exactly the same, both gameObjects have both a rigidbody and a 2D box collider.
Any help will be great.
Upvotes: 1
Views: 20330
Reputation: 2624
Better practive is to use CompareTag
instead:
private void OnCollisionEnter(Collision other) {
if(other.gameObject.CompareTag("Ground")) {
isJumping = false;
}
}
Upvotes: 1
Reputation: 66
things to check:
(and i assume you have Rigidbody2D not Rogidbody like you mentioned in your question)
(if all of these are checked then just for testing try to decrease the speed of bullet, if bullet is going too fast then try changing “Collision Detection” to continues)
most imported thing “debug” and debug before destroying not after
Upvotes: 5