- Home /
'If' Statement in javascript giving me problems
I'm trying to write a 'IF' statement for my health system heres the code I have.
var healthmin = 0;
var health = 100;
var GUIhealth : GUIText;
var text = "Health: ";
function Update(){
if (healthmin == 1)
{
health--;
GUIhealth.text = text + health.ToString();
}
else
{
GUIhealth.text = text + health.ToString();
}
function OnTriggerEnter( other : Collider ) {
if (other.tag == "zom")
{
var healthmin = 1;
}
}
function OnTriggerExit( other : Collider ) {
if (other.tag == "zom")
{
var healthmin = 0;
}
}
but its giving me that this error "Assets/playerhealth.js(21,10): BCE0044: expecting (, found 'OnTriggerEnter'." but I know there is nothing wrong with the 'OnTriggerEnter' Function because If i take the 'If' statement for the 'healthmin' on Function Update it works fine, There is something wrong with the Function Updates 'IF' Statement that I can't figure out, what am I doing wrong here?
I re-formatted the code to use better indents, which should make the error more obvious to spot.
Answer by Macdude2 · Jun 27, 2011 at 04:53 AM
I think you are missing a parenthesis after the end of your Update() function. You cannot have an on trigger enter function inside your update function also.
var healthmin = 0;
var health = 100;
var GUIhealth : GUIText;
var text = "Health: ";
function Update(){
if (healthmin == 1) {
health--;
GUIhealth.text = text + health.ToString();
}
else {
GUIhealth.text = text + health.ToString();
}
}
function OnTriggerEnter( other : Collider ) {
if (other.tag == "zom") {
var healthmin = 1;
}
}
function OnTriggerExit( other : Collider ) {
if (other.tag == "zom") {
var healthmin = 0;
}
}
Yeah, inside your OnTrigger functions you want to ditch the "var" before "health$$anonymous$$". That's re-declaring the variable which will shadow the object variable. I posted an answer with a different way to look at it.
Answer by Whimsical · Jun 27, 2011 at 04:50 AM
There's a } missing after
else { GUIhealth.text = text + health.ToString(); }
You're closing the else statement, but not the function. That's why the compiler is thrown off the path and isn't expecting OnTriggerEnter.
Please use proper code indentation. This helps you seeing these things a lot easier.
Answer by flaviusxvii · Jun 27, 2011 at 08:47 PM
var thisHurts : bool = false;
var health:float = 100.0;
var GUIhealth : GUIText;
var text = "Health: ";
function Update(){
if (thisHurts) {
health -= Time.deltaTime; // Subtracting in this way will make the damage-rate frame rate independent
}
GUIhealth.text = text + health.ToString(); // you were doing this no matter what..
}
function OnTriggerEnter( other : Collider ) {
thisHurts = (other.tag == "zom");
}
function OnTriggerExit( other : Collider ) {
thisHurts = !(other.tag == "zom");
}
Your answer
Follow this Question
Related Questions
Problem with "Pick up key" script 2 Answers
Unity MoveAround error 1 Answer
Unity3d C to Javascript conversion? 1 Answer
(10,47): BCE0044: expecting :, found '='. 2 Answers
Formatting problems in Script 2 Answers