Collision Detection, Health Variable, and Object Relocation.

I am having difficulties making a script that detects collisions between the Third Person Controller that comes with the Unity program (the character that looks like a mechanic, under the Character Controller package) and several objects that I have added (these objects all have a collider of some sort).

My intentions with it were to set a player’s health to zero. Then I have an event that is supposed to check for health being zero or less, and if that is the case, relocate the player character to a certain location (preferably either to the location of a checkpoint or the starting location in my level, but I’d like to know about both).

Unfortunately, nothing in my script seems to work, and I literally have no clue why. I am using Javascript, but I don’t have much knowledge about how to use it, so I’m completely lost. I really could use some help.

This is the script that I have been trying to make.


#pragma strict

public var playerHealth: int=100;//Determines player’s health.
var deathSound: AudioClip; //Bad sound effect that plays if the player’s health goes to 0.

function Start () {

}

function Update () {
if (playerHealth<=0)//detects whether health is less than or equal to 0 in order to activate event. It might not work.
{
playerDeath();//activates the playerDeath Event.
}

}

function WaterCollisionEnter(col : Collision){
if (col.gameObject.name == “Nighttime Simple Water”){ //A water object, with an added box collider
playerHealth=0; //Set playerHealth to 0, causing event playerDeath.
}
}
function SweeperCollisionEnter(col: Collision){
if (col.gameObject.name == “Sweeper”){ //An object with a collider.
playerHealth=0; //Set playerHealth to 0, causing event playerDeath.
}
}

function playerDeath() {
playerHealth=100; //Sets value to 100 to reset and prevent event from looping multiple times.
audio.PlayOneShot(deathSound); //Plays feedback sound. (it doesn’t play, so that probably means a collision isn’t being detected.)

/Ideally, the next code I would add would be able to set the position of the Third Person controller (the object
this script is attached to) to a defined location after the playerHealth value reaches 0.
I don’t know how to do this, however.
/
}

Please use code formatting.

What code calls SweeperCollisionEnter and WaterCollisionEnter?

Unity will automatically fire methods called OnCollisionEnter, you will probably have to combine the above two methods into this one.