Setting boolean to true if it was false but if it was false then setting to true [problem]

Hi there,
So i wanted to make like if it was true already then set to false, however if it was false then set to true.
And im having problem because when i write it it reads the code together so if its true it sets to false but when its false it doesnt sets it to true ?
Here is my code

function AddingPoints()
{
        var q = GetComponent(Player);
        if(q.dirRight == true)
        {
        q.dirRight = false;
        }
        if(q.dirRight == false)
        {
        q.dirRight = true;
        }      
}

Thanks for helping !

try q.dirRight = !q.dirRight

1 Like

Thanks for quick reply and it worked :smile: !

yw :slight_smile:

Just to quickly explain why your code wasn’t working: Try reading it out. =)

“If dirRight is true, set it to false” (if dirRight was true it is now false)
“Then, if dirRight is false, set it to true” (and we’re back to where we started!)

I fully support Munchy’s solution, but changing your last if-statement to an “else if” also would have worked. :wink:

2 Likes