If() else - Else always running, even when condition is true

Hi all!

I’ve ran into an interesting problem. I have some code inside an update(dt) function. The code is pretty simple, it looks like this:

    console.log("true branch is running");
} else {
    console.log("false branch is running");

I have tried using condition == true and condition === true and condition == 1 as well as
condition === 1.

Printing the variable into the console, shows ‘1’ for it’s value. If the condition is false, only the else branch is running. If the condition is true, then both branches are running.

Anyone has an idea what the hell is going on here? :smiley:

Hi @at_3DigitStudio,

Is there any chance this script is attached to two entities instead of one? That means that update code runs twice, for two different entities.

1 Like

HI Leonidas!

It is indeed attached to two components, but the other one is disabled.

Are you able to share a sample project that reproduces the issue to take a look?

1 Like

@Leonidas explicitly stating

} else if (this.condition === false){}

fixes the issue.

I suspect the way you are testing your condition or setting the condition value is the issue, but I have no idea without seeing a running example.

1 Like

The condition setting is pretty simple, I’ve made a function to do it:

MyScript.prototype.updateCond = function (newValue){
    this.condition = newValue;
1 Like

You will need to share your project for someone to debug it as even if this was the output in console it is possible that the program is giving the right output and the problem lies somewhere else. As its getting changed each farm or some kind of other weird thing is happening.


NDA is not an easy thing that’s for sure :smiley:

Try creating a simpler project based on the condition you are trying to put in place.

Ok, Then just puts some breakpoints in the browser and see when it is happening. And what are the state of variables.

See the references it might be attach to more components.

Yeah that’s what I started with. But again as stated in one of my replies above, explicitly stating the condition on the else part via else if, fixed the issue completely.

It does not fix the issue, but makes a workaround about the real issue. It will break later.
A number one evaluates to true in if... else... statements. Zero evaluates to false. If your if.. else.. turns around like that, it only means that the value this.condition gets a different value each frame, evaluating to true and false. You must debug and find the root cause of what is assigning an invalid value to it, rather than allowing an invalid value to be assigned and later just ignoring it - it will give you bugs that are very hard to debug later.


The update function had a part, that floats the object back to it’s original location, and turns the whole floating stuff off. The issue was that the getLocalPosition() and getPosition() reported different values, and one of the functions was not updated to use getPosition() instead of the local, hence it kept turning back on to true.

I still wonder why the log always printed true for the variable and never false.