- Home /
How can I make this only act once?
I made a script that gets when a certain variable = true, it subtracts 300 from another variable. The only problem is that it keeps subtracting and never stops. How can I make it only subtract once when it finds that it equals false? Thanks.
var LimbBroke : boolean = false;
var BodyArmature : GameObject;
var BodyMesh : GameObject;
var Blood : GameObject;
var SystemHealth : GameObject;
function Start ()
{
Blood.particleEmitter.enabled = false;
}
function Update ()
{
if(BodyArmature.GetComponent(LimbHealth).Health == 0)
{
LimbBroke = true;
BodyMesh.renderer.enabled = false;
Blood.particleEmitter.enabled = true;
SystemHealth.GetComponent(MainPlayerHealth).MaxHealth -= 300;
}
if(BodyArmature.GetComponent(LimbHealth).Health == 100)
{
LimbBroke = false;
BodyMesh.renderer.enabled = true;
}
}
Answer by ScroodgeM · Aug 13, 2012 at 05:29 PM
replace
if(BodyArmature.GetComponent(LimbHealth).Health == 0)
with
if(BodyArmature.GetComponent(LimbHealth).Health == 0 && !LimbBroke)
or declare other variable and set it to another value on first executing, and don't execute again if variable in new state
Ah, I see! It seems very clever even if it has been around for a long time.
Answer by Dev Solo · Aug 13, 2012 at 06:27 PM
Change your if statement on the first one to this: it should work.
if(BodyArmature.GetComponent(LimbHealth).Health == 0 && LimbBroke == false)
Answer by Dragonlance · Aug 13, 2012 at 06:41 PM
Do you really have to check the health every frame?
Why not have an applyDmg(float dmgValue) function.
Like this
var LimbBroke : boolean = false;
var BodyArmature : GameObject;
var BodyMesh : GameObject;
var Blood : GameObject;
var SystemHealth : GameObject;
function Start ()
{
LimbBroke = false;
BodyMesh.renderer.enabled = true;
Blood.particleEmitter.enabled = false;
}
function Update ()
{
}
public function applyDmg(healValue : float)
{
var health : float = BodyArmature.GetComponent(LimbHealth).Health;
if(health > 0.0f)
{
health -= dmgValue;
if(health <= 0.0f)
{
LimbBroke = true;
BodyMesh.renderer.enabled = false;
Blood.particleEmitter.enabled = true;
SystemHealth.GetComponent(MainPlayerHealth).MaxHealth -= 300;
BodyArmature.GetComponent(LimbHealth).Health = 0.0f;
}
else
{
BodyArmature.GetComponent(LimbHealth).Health = health;
}
}
}
function applyHealing(healValue : float)
{
var health : float= BodyArmature.GetComponent(LimbHealth).Health;
if(health < 100.0f)
{
health += healValue;
if(LimbBroke == true && health >= 100.0f)
{
LimbBroke = false;
BodyMesh.renderer.enabled = true;
BodyArmature.GetComponent(LimbHealth).Health = 100.0f;
}
else
{
BodyArmature.GetComponent(LimbHealth).Health = health;
}
}
Your answer
Follow this Question
Related Questions
Having trouble checking if the player is in a trigger. 2 Answers
What am I doing wrong with this bool? 3 Answers
How does Unity reads the "If" conditions? 3 Answers
Set a global bool to true? 1 Answer
Use of boolean functions in c# 1 Answer