- Home /
Other
Health bar takes damage
I've got a script that makes the player have a GUI bar on the screen and I need a script that takes damage off the player.
Script :
var h00 : Texture2D;
var h10 : Texture2D;
var h20 : Texture2D;
var h30 : Texture2D;
var h40 : Texture2D;
var h50 : Texture2D;
var h60 : Texture2D;
var h70 : Texture2D;
var h80 : Texture2D;
var h90 : Texture2D;
var h100 : Texture2D;
static var HEALTH = 100;
function Update()
{
if(HEALTH > 100)
{
g_Health.guiTexture.texture = h100;
return;var g_Health = gameObject.Find("g_Health");
}
else if (HEALTH > 90)
{
g_Health.guiTexture.texture = h90;
return;
}
else if (HEALTH > 80)
{
g_Health.guiTexture.texture = h80;
return;
}
else if (HEALTH > 70)
{
g_Health.guiTexture.texture = h70;
return;
}
else if (HEALTH > 60)
{
g_Health.guiTexture.texture = h60;
return;
}
else if (HEALTH > 50)
{
g_Health.guiTexture.texture = h50;
return;
}
else if (HEALTH > 40)
{
g_Health.guiTexture.texture = h40;
return;
}
else if (HEALTH > 30)
{
g_Health.guiTexture.texture = h30;
return;
}
else if (HEALTH > 20)
{
g_Health.guiTexture.texture = h20;
return;
}
else if (HEALTH > 10)
{
g_Health.guiTexture.texture = h10;
return;
}
else if (HEALTH <= 0)
{
g_Health.guiTexture.texture = h00;
Application.LoadLevel(0);
return;
}
}
And heres the Cube Attack.js Script :
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "Player")
hit.transform.gameObject.GetComponent(Player).Substract();
}
function Substract() {
Player.HEALTH -= 10;
if(Player.HEALTH <= 0);
Debug.Log("Dead - Script Works Fine");
}
Can anyone post a script please I'm only 12.
Felipe
Answer by adrenak · Mar 13, 2012 at 12:50 PM
Do you want something to be able to decrease the health? For that you will have to access the HEALTH variable that you have there and decrease it .
I take here a scenario : there is a cube. every time you enter the cube, your health decreases by 10. I assume that your player is tagged as "Player" and the script above you have is named "healthScript" and it is attached to a FirstPersonCameraController, and that the cube is a trigger collider.
Here's the kind of code you would require for the cube :
function OnTriggerEnter ( other : Collider) {
if(other.gameObject.tag == "Player")
other.transform.root.gameObject.GetComponent(healthScript).decreaseHealth();
}
Then you will have to make an addition to the script that you have above :
function decreaseHealth () {
HEALTH -= 10;
if(HEALTH <= 0){
Debug.Log("DIE");
}
Sorry there, Also the console shows -$$anonymous$$ Identifier "HEALTH"-. The script works fine except for that.
As your variable is static,if HEALTH is in healthScript, where you do your collision use healthScript.HEALTH .
$$anonymous$$ake sure you test the script with your project and you will get the same problem
Follow this Question
Related Questions
GetComponent Help 2 Answers
GUI.Button carrying out two functions? 1 Answer
I am trying to make a stamina bar and it won't drain no matter what i try. 1 Answer
How to slowly decrease health script? 3 Answers
Need help converting unityscript to c# 5 Answers