Help me with health script
Hi guys, I'm new at scripting. I'm trying to make a script that will reduce the life of the first person controller. This is the script of my health manager:
static var health : int = 0;
var myCol : Transform;
var collided : int = 0;
var target : Transform;
var myTransform : Transform;
var zero : int = 0;
var one : int = 1;
var two : int = 2;
var three : int = 3;
var four : int = 4;
var five : int = 5;
var six : int = 6;
var seven : int = 7;
var eight : int = 8;
var nine : int = 9;
var ten : int = 10;
var healthguitext : GUIText;
var zombie : Transform ;
function Start(){
health = 10;
}
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.collider.gameObject.tag == "zombie") {
health--;
}
}
function Update(){
if (health == 10){healthguitext.text = ten.ToString();}
else if (health == 9){healthguitext.text = nine.ToString();}
else if (health == 8){healthguitext.text = eight.ToString();}
else if (health == 7){healthguitext.text = seven.ToString();}
else if (health == 6){healthguitext.text = six.ToString();}
else if (health == 5){healthguitext.text = five.ToString();}
else if (health == 4){healthguitext.text = four.ToString();}
else if (health == 3){healthguitext.text = three.ToString();}
else if (health == 2){healthguitext.text = two.ToString();}
else if (health == 1){healthguitext.text = one.ToString();}
}
function lessdamage(){
if(collided == 0) {
health--;
collided++;
yield WaitForSeconds(1);
collided--;
}
}
I tried inserting the collision in the zombie script, but it counts too much collisions and my life goes to 1 in 1 millisecond
Answer by Seth-Bergman · Nov 08, 2012 at 10:13 PM
you could get rid of all that extra code, in Update it should just say:
function Update(){
healthguitext.text = health.ToString();
}
Well, one way to do this would be to create a coroutine using yield WaitForSeconds() as you have attempted here.. I believe this should work fine, you just have to update the Collision function:
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.collider.gameObject.tag == "zombie") {
lessdamage();
}
}
So, here is the new version:
static var health : int = 0;
var collided : boolean = false;
var healthguitext : GUIText;
function Start(){
health = 10;
}
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.collider.gameObject.tag == "zombie") {
LessDamage();
}
}
function Update(){
healthguitext.text = health.ToString();
}
function LessDamage(){
if(!collided) {
health--;
collided = true;
yield WaitForSeconds(1);
collided = false;
}
}
this should work
Answer by cullmejake · May 22, 2016 at 08:22 AM
another way to convert to a string is to type health += "";
Your answer
Follow this Question
Related Questions
CS8025 Parsing error!? 1 Answer
Help with my Arrow Fire Script 1 Answer
Can't add script problem 13 Answers
Lag Problem in 2D Game 1 Answer
Null reference exception object reference not set in an instance of an object 1 Answer