- Home /
Damage System Doesnt Work?
Newbie Help please. I am trying to write a simple command that reduces my health by 10 every time i press A:
var Health : float = 100;
var MaxHealth : int = 100;
var HealthBarTexture: Texture2D;
var SelfDamage : int = 10;
function TakeDamage ()
{
if(Input.GetKeyDown(KeyCode.A))
{
Health -= 10;
}
}
function OnGUI()
{
if (Health <= 0)
{
Health = 0;
}
var HealthPercent = Health / MaxHealth;
var left = 50;
var top = 50;
var MaxHealthBarWidth = 500;
var HealthBarWidth = MaxHealthBarWidth * HealthPercent;
var height = 50;
//Health Bar Texture
GUI.DrawTexture(Rect(left,top,HealthBarWidth,height), HealthBarTexture, ScaleMode.StretchToFill, true, 1.0);
}
Help please? Thanks
Comment
Best Answer
Answer by Landern · Aug 22, 2014 at 02:02 PM
Well, when is the function/method TakeDamage called? Replace TakeDamage with Update so you can check for the input key press each frame.
var Health : float = 100;
var MaxHealth : int = 100;
var HealthBarTexture: Texture2D;
var SelfDamage : int = 10;
function Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
Health -= 10;
}
}
function OnGUI()
{
if (Health <= 0)
{
Health = 0;
}
var HealthPercent = Health / MaxHealth;
var left = 50;
var top = 50;
var MaxHealthBarWidth = 500;
var HealthBarWidth = MaxHealthBarWidth * HealthPercent;
var height = 50;
//Health Bar Texture
GUI.DrawTexture(Rect(left,top,HealthBarWidth,height), HealthBarTexture, ScaleMode.StretchToFill, true, 1.0);
}
OR
var Health : float = 100;
var MaxHealth : int = 100;
var HealthBarTexture: Texture2D;
var SelfDamage : int = 10;
function Update ()
{
if(Input.GetKeyDown(KeyCode.A))
{
TakeDamage();
}
}
function TakeDamage ()
{
Health -= 10;
}
function OnGUI()
{
if (Health <= 0)
{
Health = 0;
}
var HealthPercent = Health / MaxHealth;
var left = 50;
var top = 50;
var MaxHealthBarWidth = 500;
var HealthBarWidth = MaxHealthBarWidth * HealthPercent;
var height = 50;
//Health Bar Texture
GUI.DrawTexture(Rect(left,top,HealthBarWidth,height), HealthBarTexture, ScaleMode.StretchToFill, true, 1.0);
}
Answer by BlitzSam · Aug 22, 2014 at 02:05 PM
EDIT: Fixed it. Had to use "function update ()". TIL that "function update ()" is a command. DERP
Your answer
Follow this Question
Related Questions
Newbie Question Regarding Variables 2 Answers
Getcomponent Javascript error 3 Answers
Errors CS1513 and CS1022 - Need help. 1 Answer
Damage the Player 1 Answer
Vector3 Error - "The type or namespace 'Vector3' could not be found." 0 Answers