- Home /
WHY WON'T YOU DIE?! (Why won't the OnCollisionEnter function run?)
Hi, I have a problem. See, I'm trying to make it so that when you fall in water, you die. (I don't have Unity Pro, so I'm just using the simple water thing, adding an object under it.) Here's the script I have:
function OnCollisionEnter(death:Collision)
{
Debug.Log("Collide with thing");
if(death.gameObject.tag == "Enemy")
{
Debug.Log("DIE");
Application.LoadLevel("dead");
}
}
Does anyone know what the problem is, and how to fix it? The object DOES have the Enemy tag. Also, this script does work with other things, like the enemies that run towards you.
does "Collide with thing" run?
does the water itself have collider by accident?
does the object with the enemy-tag have Is trigger UNchecked?
Is Enemy tag Spelled correctly(copy and paste it)
(trust me it can really fool you around)
I've checked and rechecked everything on that list. I'm so confuuuuuused!
Oh, and no, "Collide with thing" doesn't run.
Where is this script attached? Is the water trigger? Does your guy have a rigidbody, even is$$anonymous$$inematic?
Answer by rodude123 · Nov 10, 2013 at 08:45 AM
Try this example
function OnTriggerEnter (other : Collider) {
Debug.Log("Collide with thing");
if(death.gameObject.tag == "Enemy") {
Debug.Log("DIE");
Application.LoadLevel("dead");
}
}
this should help this is in JS
For this to work, the water plane must be a trigger and must have this script attached, and the character must be a CharacterController or a rigidbody.
what about OnControllerColliderHit ?
afaik controller and OnTriggerEnter don't work well together
Answer by Aspirer · Nov 10, 2013 at 09:42 AM
Most likely, your player's collider's "Is Trigger" is checked, for the sake of other functions in your game. When IsTrigger is checked, none of the OnCollision functions are called--instead, "OnTriggerEnter", "OnTriggerStay" and "OnTriggerExit" are called.
Answer by ZDS Alpha · Nov 10, 2013 at 11:10 AM
You are using FPS Character Control. Am I right? Look this:
http://answers.unity3d.com/questions/571448/oncollisionenter-is-not-working-1.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613121552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision with no contact? 2 Answers
Unparent object on collision? 3 Answers
Transform collider not detecting collision on rigidbody collider 2 Answers
onCollisionEnter function called from another object. 1 Answer
Change Direction on Collision 6 Answers