- Home /
OnCollisionEnter() not firing when collision occurs
Both objects have a Rigidbody attached to them
Neither objects Rigidbody component are "Kinematic"
The two objects have box colliders attached, not mesh colliders
The script is being called from the object with the box collider attached
Both object layers are ticked in the Settings > Physics panel
Neither box colliders have "Trigger" set to true
The objects cannot pass through each other in world space
The code is attached to a prefab that is already instantiated in the world.
However, the following code does not execute:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Entity")
{
hitDetected = true;
collision.gameObject.GetComponent<EntityBehaviour>().isDead = true;
GameObject.Find("PlayerController").GetComponent<ShipController>().AddScore(100);
}
}
There is no "Collision!" appearing in the Debug log, and none of the code runs.
Unity 5 is not broken. What am I missing?
Answer by Epsilon_Delta · Mar 28, 2018 at 10:37 AM
The message parameter has to be of type Collision, not Collider. Unity console should show you this error.
No error in console.
Changed to "collision" as parameter and still not working.
Now you don't have Debug.log("Collision!") in your code. Anyway, the code you provided is working fine, error must be somewhere else. Are you spelling the name "OnCollisionEnter" correctly in the code? Are you using 3D physics or 2D physics (it would be OnCollisionEnter2D in that case) ? Isn't the rigidbody child of another rigidbody? Have you saved your code? Have you applied changes to the prefab?
3D Physics
There is only one rigidbody
Yes I saved it
Yes I have rewritten the script twice
Answer by melsy · Mar 29, 2018 at 04:15 AM
Is the object tagged "Entity". Just checking. Sometimes the smallest things slip thru the cracks.
Your answer
Follow this Question
Related Questions
FIXED: How can I stop a collider from tunneling? 2 Answers
Show trajectory path a ball will take based on collision 1 Answer
What is the best way to prevent a physics object from going through thin colliders? 2 Answers
Unity3D: Get Velocity after collision 0 Answers
Make Ray hit own collider 1 Answer