On Collision add life

Hello again guys,

I’ve been trying to add a collision between a player and a sanwich to give him life. But when player touched the sanwich, he just goes straight through it with no collision or addition to life. Perhapsd I haven’t turned anything on… heres my codes for that bit anyway

function OnCollisionEnter(otherObject: Collision){
	if(otherObject.gameObject.tag == "health"){
		playerScript.playerLives+=1;
		Destroy(gameObject);

I was using onTriggerEnter, but that didn’t work either. Any ideas?

If you’re using the Character Controller, you must use OnCharacterColliderHit instead of OnCollisionEnter. Take a look at:

http://unity3d.com/support/documentation/ScriptReference/CharacterController.OnControllerColliderHit.html

Triggers work too, but you should assign the script containing OnTriggerEnter to the trigger object, not to the character.

Saldy, i’d forgotten to say that my health object was a trigger (yep I forgot to tick that box)

but below is some more revised code, that may help someone in the future:

function OnTriggerEnter(otherObject: Collider){
		
	if(otherObject.gameObject.tag == "health"){
		playerLives ++;
		}
		}

Simple, and easy to understand.