How to use if statement with var?

Hi, I’m recieving this error when I attempt to use an if statement with a variable. BCE0044: expecting (, found 'var'.
I’m getting this problem with this code. if var Activate = true;

Can anyone help me please?

3 Answers

3

you can’t declare a new variable when using an if statement

var activated : boolean = false;

function Update(){
  if(activated){
    ...
  }
}

Several small mistakes here. As unluckyBastard pointed out, you cannot declare a variable inside a condition, it makes sense, if you think about it…

Also, you’re using a single equal symbol ‘=’ which will assign a value rather than compare. You mean to use ‘==’.

Lastly, the statement to evaluate in a condition must be in-between parenthesis. Hence your code could look like this:

var Activate = true;
if(Activate==true){
    //do something
}

OR(cleaner)

var activate:boolean = true;
if(activate){
    //do something
}

Thanks unluckyBastard and BY0LOG1C.