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.

Using character controller? Any rigidbodies involved?

Where is this script attached? Is the water trigger? Does your guy have a rigidbody, even isKinematic?

4 Answers

4

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

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.

If your “death” object uses a mesh collider, you may need to set convex to true in the Mesh Collider inspector settings.

You are using FPS Character Control. Am I right? Look this: