- Home /
Unity If statement issue (It goes through the function without meeting the parameters for the if statements)
Okay so I have the basics down but I'm still new to programming with unity. I'm creating a simple fps and I'm at the point where I'm doing my enemy scripting. I thought I created a script that made sense for the enemy health and when the enemy dies but it's not working correctly.
var enemyHealth = 50;
function Start(){
enemyHealth = 50;
print("Why is this messed up?");
}
function OnCollisionEnter(collider : Collision) {
if(collider.gameObject.tag == "Bullet");
enemyHealth -= 10;
};
//Heres my issue
function EnemyDie(){
if (enemyHealth == 0);
Destroy(gameObject);
print("Function Worked!");
}
function Update(){
EnemyDie();
}`
So this keeps destroying itself, The EnemyDie() function is where it happens. If I comment that out, the script runs fine but doesn't destroy itself after. The other function works as it is supposed too, the enemy doesn't take damage unless it gets hit with a bullet. I've tried many variations and cannot figure this out, please help. Thank you.
Note * I made an even simpler script to test out the concept of my code and still had the same issue.
var Health = 50;
function Start () {
Health = 50;
}
function Update () {
if (Health <= 0);
print("Hello World!");
}
The parameter to print "Hello World" is that Health has to be less than or equal to 0. Health is at 50 and there's no way in this function to lose health, and yet somehow, it prints "Hello World!"
I'm so confused.
Answer by tw1st3d · Aug 01, 2013 at 06:02 AM
Just gonna go ahead and fix these from start to finish.
var enemyHealth = 50;
function Start()
{
enemyHealth = 50;
print("Why is this messed up?");
}
function OnCollisionEnter(collider : Collision)
{
if(collider.gameObject.tag == "Bullet")
enemyHealth -= 10;
}
function EnemyDie()
{
if (enemyHealth <= 0)
{
Destroy(gameObject);
print("Function Worked!");
}
}
function Update()
{
EnemyDie();
}
var Health = 50;
function Start()
{
Health = 50;
}
function Update ()
{
if (Health >= 0)
print("Hello World!");
}
Basically, if statements don't need ; at the end of the line, because it's a conditional, not a procedure.
Okay, makes sense. When I take away the ";" from the collision function, it stops working, but when I take away ";" from the EnemyDie() function, it works the way I need it, so thank you.
What you're thinking of with doing }; is ending a C# class, javascript wouldn't use that for a function. No idea why that would cause your function to not work.
You're guess is better than $$anonymous$$e, so I don't know. But regardless, everything is working, so thank you for the help! :D
Answer by dorpeleg · Aug 01, 2013 at 06:04 AM
You are not writing the if statements correctly.
When doing 1 line under an if stament you can write it like this:
function Update () {
if (Health <= 0)
print("Hello World!");
}
*notice there is no ';' after the if.
If you are doing more then 1 line under the if statement, you should write it like this:
function EnemyDie(){
if (enemyHealth == 0){
Destroy(gameObject);
print("Function Worked!");
}
}
*notice there is no ';' after the if and the added '{}'.
This is basic programming, not only in unity.
I took a program$$anonymous$$g class and attempted to $$anonymous$$ch myself, somewhere I received bad information and was never corrected. I took away the ";" and it broke one function but fixed the other, simple solution just keep it for the one I need updated and remove it for the EnemyDie(), thank you for the help!
If this helped you, please mark the as answered.
(you can mark both of the answers if you like :) )
Your answer
![](https://koobas.hobune.stream/wayback/20220613111326im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with gun scripting!? 1 Answer
I have a Multiplayer script here 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Inup.GetButtonDown not working? 1 Answer