- Home /
OnCollisionEnter2D isn't working
I have two objects, both of them have rigidbody2d set to dynamic and istrigger is off. Both the objects when collided on screen register the collision and do not pass through each other. But the code under the void OnCollisionEnter2D(Collision2D col) isn't working at all. The spelling is correct. Please help. I'm posting the whole code from the script. Thank you!
public TimeManager timeManager;
public Animator anim;
public Animator anim1;
public static Plane instance;
void Awake()
{
if (instance == null)
{
instance = this;
}
}
void Start () {
}
void Update () {
if (Input.GetButton("Fire1"))
{
SlowTime();
}
}
void SlowTime()
{
timeManager.SlowMotion();
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet")
{
anim.SetTrigger("Hurt");
anim1.SetTrigger("KnockBack");
Debug.Log("Hello");
}
}
}
Put some debug above the tag check, that's the most likely culprit right now.
I've added the debug above the tag check but it still doesn't work
So you setup your game objects wrong. Please upload screens of hierarchy of both objects that are supposed to collide
Answer by SonicDirewolf · Oct 11, 2018 at 02:14 PM
I just figured it out. It was very stupid of me. The object with the code and the object with the box collider were different but under the same parent. I added the box collider to the object with the script and it's working great now. Thank you so much for your time!
Your answer
![](https://koobas.hobune.stream/wayback/20220612180122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HOW TO CALLBACK COLLIDER WITH NAME? 2 Answers
OnCollisionEnter2D Problems in Unity 4.5 1 Answer
OnCollisionEnter2D and OnCollisionExit2D issue 1 Answer
More realistic physics on child collisions 2D 0 Answers
Buggy touch controls? 0 Answers