OnCollisionEnter not working

for some reason, this isn’t working

void OnCollisionEnter (Collision hit) {
	//if (hit.impactForceSum.magnitude > 10) {
		Debug.Log("yay");
	//}
}

its not registering the collision and saying yay in the console

Edit: i forgot to tell you, its a capsule collider, and isTrigger is false

Is your other collider having rigidbody or any rigidbody attached to this collider. On colllision will only be called when one of the colliders have a rigidbody. Take a look at this. OnCollisionEnter.

8 Likes

ah, thats what is missing, thanks man

new problem, now its colliding with something i really dont want it to, ill try something and see if it works in the future…

You can ignore collisions with Physics.IgnoreCollision or Physics.IgnoreLayerCollision.

Yeah, i know about that, but if i try that, that takes more time to set up, i just decided to do it a bit differently

If you set the other collider to be a trigger only, it should work, if I understand your issue correctly. That is what the isTrigger is for.
You may also need to set the new collider to isKinematic, though I don’t see that changing the way the collider works, just to stop it from being processed by PhysX.

Another possible issue: The order of Rigidbody and Collider components on the game object. This requirement is not documented on the OnTriggerEnter page. Tested on 2017.4.

The Rigidbody component must be added first, followed by the Collider component. The order of already added components can be changed in the inspector cogwheel dropdown with “Move Up”/“Move down”.

4 Likes

You are a hero today sir

1 Like

your collider is too small. too small.Increase your size.

Might be a silly answer, but sometimes the little things are missed.

I ended up on this post and couldn’t figure it out. Come to find out, I forgot to attach the script to my projectile gameObject.

Make sure your script is attached! =D

3 Likes

also make sure the rigidbody is not kinematic

4 Likes

Hey all, I have been having similar issues and found this page. For me I had a prefab that had child objects(Camera, Lamp, Cylinder). I had my script on the Parent prefab and OnCollisionEnter was not registering…It turns out the Rigidbody was not registering with the floor because the Ridgidbody was on the Cylinder child object…not the parent prefab. Even with dragging the Cylinder Child object into the inspector as the Rigidbody to use, still no register with the ground…I put a small script on the Cylinder Child object and dragged in the same Rigidbody and walla…Now it is registering as touching the ground.

i know this is old but i just found a solution to a problem in my code, i had OnCollisionEnter in a script attached the parent of the thing that was getting the collision, not the thing that was getting the collision, so it didn’t work.

obvious really but if you are getting frustrated, check you have the script attached to the correct object

1 Like


Thank You!

I just had this when following a tutorial, turns out that the gameObject.name is important and needs to match your object that is doing the collision (‘player-ball’ in my case). I had just typed the name in wrong.

1 Like

i just want to say. Love u bro

None of these work for me :frowning:

Another possible issue, even if a bit silly:
If you make 2D game make sure you use void OnCollisionEnter2D(Collision2D collision) not OnCollisionEnter.

14 Likes

I’m the luckiest person in the world.
because This is the only article you have written. and it has been less than 24 hours since you wrote it.
For me, this is a very simple and accurate diagnosis.

You saved my time. TY.

3 Likes