- Home /
Why won't my death function work?
var curHealth : int = 100; var maxHealth : int = 100; var healthtext : GUIText;
function Start () {
} function Update () { healthtext.text = "Health: " + curHealth + " / " + maxHealth; if(curHealth < 0 ) { curHealth = 0; } if(curHealth > 100) { curHealth = 100; } if(curHealth <= 0) { Dead(); } function Dead() { transform.position = Vector3 (1, 2.5, 1); curHealth = maxHealth; } }
Answer by ncallaway · Jan 03, 2014 at 04:15 AM
I'm guessing you're seeing syntax errors based on the code-snippet you posted. When you say your death function doesn't work are you seeing the following syntax errors?
BCE0044: expecting (, found 'Dead'.
UCE0001: ';' expected. Insert a semicolon at the end.
If so, then the problem is the (accidental?) nesting of the Dead function within the Update function. I've reformatted the original snippet that you've posted (with the Start function removed). Note how the Dead() function is actually nested within the Update() function:
var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;
function Update () {
healthtext.text = "Health: " + curHealth + " / " + maxHealth;
if(curHealth < 0 ) {
curHealth = 0;
}
if(curHealth > 100) {
curHealth = 100;
}
if(curHealth <= 0) {
Dead();
}
function Dead() {
transform.position = Vector3 (1, 2.5, 1);
curHealth = maxHealth;
}
}
The syntax errors will disappear if you change the order of your curly braces, so that Dead() is no longer nested within the Update() function, it looks something like this:
var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;
function Update () {
healthtext.text = "Health: " + curHealth + " / " + maxHealth;
if(curHealth < 0 ) {
curHealth = 0;
}
if(curHealth > 100) {
curHealth = 100;
}
if(curHealth <= 0) {
Dead();
}
}
function Dead() {
transform.position = Vector3 (1, 2.5, 1);
curHealth = maxHealth;
}
If the syntax errors weren't your problem, would you clarify what you meant by "my death function won't work"
Thank you, I found that out now. Thank you very much for your feed back. How do you post code like that on here?
Look on the right hand side of the page for the link that says "To help users post good questions and use the site effectively we have posted a tutorial video. Please check it out."
It will also show you how to post comments as comments, not as answers, and how to mark answers as correct... I've done it for you this time ;)
Your answer
Follow this Question
Related Questions
health problem 1 Answer
Health does not update in this script, why? 1 Answer
Can someone write me a health script? 2 1 Answer
I need Help with the bergzerg arcade Melee combat scripts 1 Answer