0
\$\begingroup\$

I have two game objects. They both have a Rigid Body 2D and a Box Collider 2D components.

One of them also has script component. Below is the full script

using UnityEngine;
using System.Collections;
public class Col : MonoBehaviour
{
void Start ()
{
}
void Update ()
{
}
void onCollisionEnter2D(Collision2D col)
{
Debug.Log("collision detected");
}
}

When I start the project, go to the Scene view and drag one GameObject over another i don't get the Log message i am expecting to see.

What am i doing wrong here? Seems so simple.

Last pic is the size of the box colliders so you can see that they are correct size.

I've seem other very similar question and they are all in regards to unity5.0 (i am using 5.0 as well)

enter image description here enter image description here enter image description here

\$\endgroup\$
  • \$\begingroup\$ Is there some reason you want to delete this question? Did the answer below solve your problem? If not, why don't you post how you solved it as your own answer? Remember that this site isn't just for your personal use, the questions and answers are here to benefit everyone. \$\endgroup\$ – MichaelHouse Mar 22 '15 at 23:18
0
\$\begingroup\$

Feel weird answering my own question but the problem is the

void onCollisionEnter2D(Collision2D col)

It needs to be

void OnCollisionEnter2D(Collision2D col)

(With the capital 'O').

Hope this help someone over there as it is rather easy to overlook for beginners

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That's what my answer said, and you said it didn't help. Read my answer again. I said your method signature was incorrect. \$\endgroup\$ – MichaelHouse Mar 22 '15 at 23:22
  • \$\begingroup\$ i just caught the 2D part of your answer and i had that part already fixed before you posted it. I had to dig elsewhere to catch the 'O' part. Thanks for everything and sorry about our little scuffle, no hard feelings hopefully. \$\endgroup\$ – Dust Mar 22 '15 at 23:25
  • \$\begingroup\$ My fault, I only called out the 2D change and not the capital O as well. Thanks for getting everything in order and modifying this question to be more clear. \$\endgroup\$ – MichaelHouse Mar 22 '15 at 23:27
  • \$\begingroup\$ All good, i suspected it was something simple. \$\endgroup\$ – Dust Mar 22 '15 at 23:30
0
\$\begingroup\$

Collisions in the physics space (stuff using onCollision, not onTrigger) need to have a Rigidbody2D attached to the objects. This Rigidbody2D component is what places the objects into the physics simulation and allows for the objects to collide with each other.

If you only want to know when the objects touch each other, and not simulate physics or collision responses, use onTriggerEnter2D (Making sure to set your colliders to triggers).

Also, you'll need to ensure your method signatures are correct:

void onCollisionEnter2D(Collision col)

is wrong. You want

void OnCollisionEnter2D(Collision2D coll)

Notice the 2D type of collision is the correct way to implement this method, in addition to the capital O at the beginning.

I started a new scene. Added two 2D sprites. I added colliders to both. I then added a rigid body to one and attached a new script to the other. The script contained your code above for OnCollisionEnter2D. When I moved the rigidbody object over the other object, the debug text was printed.

You either need to start over because you've done something wrong or you need to reexamine your assumptions.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.