My issue is that I am using the script so that when the object i place it on (an invisable box) is collided with it respawns you back at start and takes away a life. Problem comes in that when you have 6 boxes in game with the script on each you can hit each box 3 times before you die, not 3 life in general. I want it so that if u hit any of the boxes it removes a life.
var spawnPoint : Transform;
//var theShadow : GameObject;
public var life : int = 3;
function OnTriggerEnter (other : Collider)
{
other.gameObject.active = false;
//theShadow.active = false;
yield new WaitForSeconds (0.5);
other.transform.position = spawnPoint.position;
life -= 1;
if(life == 0) Application.LoadLevel("End Game");
other.gameObject.active = true;
// theShadow.active = true;
}
function OnGUI(){
GUI.Label (Rect (300, 100, 200, 30), "Life: " +life);
}
The problem here is that the box itself is holding the 'lives' value, not the player! You need to split this functionality off into two different scripts. So, on your box script, do this-
function OnTriggerEnter (other : Collider)
{
other.SendMessage("Die", SendMessageOptions.DontRequireReceiver);
}
Then on your player, do this-
var spawnPoint : Transform;
//var theShadow : GameObject;
public var life : int = 3;
function Die()
{
life -= 1;
if(life == 0)
{
Application.LoadLevel("End Game");
} else {
StartCoroutine(DeactivateRespawn());
}
}
function DeactivateRespawn ()
{
gameObject.active = false;
yield new WaitForSeconds (0.5);
transform.position = spawnPoint.position;
gameObject.active = true;
}
function OnGUI(){
GUI.Label (Rect (300, 100, 200, 30), "Life: " +life);
}
This way, the box tells the player to die and respawn, instead of the box doing the job itself. The player should know how to do this job better than a box should.