- Home /
Detecting collision with OnCollisionEnter2D not working
Hey,
I'm making a 2D "Avoid the Walls" game. I want a script that's on the player to detect a collision with a wall, as well as a wall checkpoint (which check if the player has passed a wall, and counts 1 towards the score, etc.). I have tried everything but just can't get the script to detect the walls or the checkpoint.
The objects are made as followed:
- The wall pieces are both 2D sprites with a box collider 2D that is not a trigger. It also contains a kinematic rigidbody 2D
- The wall checkpoint is exactly the same as the wall pieces, except is doesn't have a sprite.
- The player is a 2D sprite with a circle collider 2D that is not a trigger. It also contains a kinematic rigidbody 2D.
This is my script (I cut out the unnecessary parts):
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag.Equals("Wall") == true) {
Debug.Log("Collision");
// Game Over Screen //
}
if (collision.gameObject.tag.Equals("WallCheckpoint") == true) {
score++;
Debug.Log("Wall Passed, Score is " + score);
}
}
Does anybody know what might be happening here? Any help is greatly appreciated!
EDIT: "score" is an int variable that is defined before Start()
Answer by mamuelSraz · Nov 08, 2019 at 08:19 PM
i dont know but i would be using if(collision.gameObject.tag=="Tag");
maybe would help?
$$anonymous$$aybe. However, I've used this syntax before in a different project and it worked just fine. I will try it out, though
Your answer
Follow this Question
Related Questions
Bullet hit the collider of a child object 1 Answer
OnCollision2D will not work for me 1 Answer
Best way to manually calculate collision between GameObjects and a Tilemap without using physics 1 Answer
Creating a "Connection" between two game objects using another game object. 0 Answers
Small 2D collider passing through other thin collider 2 Answers