- Home /
OnCollsionEnter2D not getting called
I have 2 objects, one that moves down at a constant velocity and a player controlled object. Both objects have BoxCollider2D and Rigidbody2D yet the OnCollisonEnter2D method attached to the controllable object isnt being called. Both the objects are colliding visibly colliding with each other and will rotate so long as freeze rotation is not on. Bellow is the code for the OnCollisonEnter2D method. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collisons : MonoBehaviour
{
public int Score;
public void OnCollisonEnter2D(Collider2D collision)
{
GameObject collider = collision.gameObject;
Debug.Log("Collided");
//checks to see if the block is correct;
if (collider.GetComponent<BlockBehavior>().correct)
{
Destroy(collision.gameObject);
Score++;
}
else
{
this.gameObject.GetComponent<Movement>().gameOver = true;
}
}
}
Why do you post the same question 3 times? I removed your spam.
$$anonymous$$y bad, when I published the question it gave me a 540 error I think. It was either a 540 or a 504 so I thought the question wasn't published
Answer by imrexzombie · Jun 18, 2020 at 06:31 AM
Check in the ispector if the IsTrigger checkmark is enabled o not. You should also check if the BlockBehavior>().correct variable is actually true And you call also check if the collision detecting mechanism is working correctly by checking other values from the collision object... like the "tag" for example
public void OnCollisonEnter2D(Collider2D collision)
{
GameObject collider = collision.gameObject;
Debug.Log("Collided");
//checks to see if the block is correct;
if (collision.gameObject.tag == "Block-A")
{
Destroy(collision.gameObject);
Score++;
}
else
{
this.gameObject.GetComponent<Movement>().gameOver = true;
}
}
Your answer
Follow this Question
Related Questions
if statement not working when detecting collision between two prefabs 1 Answer
Disable collision before collision actually happens 1 Answer
Collision detection from specific directions using rigid body character? 0 Answers
Check collision above player with raycasthit2d array 1 Answer
Having problems with obstacle collision killing the player 1 Answer