[SOLVED] Function contained in if statement fires when it should not

This is my beautiful code:

var Lasergun = pc.createScript('lasergun');
Lasergun.attributes.add('daray', {
type: 'entity'
});
Lasergun.prototype.initialize = function() {
this.canfire = true;
(this.daray).enabled = false;
};
Lasergun.prototype.update = function(dt) {
if(this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT))
{

// BROKEN PART SHOULD NOT FIRE
if(1 === 2);
{
(this.daray).enabled = true;
this.entity.sound.play("sound");
Bullets -= 1*dt;
}

}
else
{
(this.daray).enabled = false;
Bullets += 1*dt;
}

if(Bullets > 1)
{
Bullets = 1;
}
if(Bullets < 0)
{
Bullets = 0;
this.canfire = false;
}
console.log(this.canfire)
};

And yet, for some reason the if statement contained within the click check fires regardless of what I do. The click check works just fine, but the if statement within that other if statement seems to get ignored. Notice how I have the if statement check if 1 === 2. Obviously it is not but yet it runs anyway. Nothing I put in the if statement will make it not run. Why?

Semicolon in the end of line 14?

In JavaScript, a semicolon terminates the if statement, meaning the if statement ends right there, with no code block associated with it.
This is why your if condition is not working. Remove the semicolor from line 14 and it will work as intended.

1 Like

Add that to the list of stupid shit I wont remember and will suffer from in the future.