- Home /
'else' won't work in an if statement
Here is my code:
#pragma strict
var walkSpeed = 0.2;
var runSpeed = 1;
var flySpeed = 3;
function Start () {
}
function Update () {
if (Input.GetAxis("Vertical"));
animation.CrossFade("Walk_") = walkSpeed;
else
animation.CrossFade("Rest");
}
Whenever I try to use 'else' this comes up:
BCE0044: expecting }, found 'else'.
Is there a new code for if statements instead of else? I've heard you use OR or || for something in if statements but none seem to work for me...
Does anyone have an answer?
Thanks
You have a semicolon where it should not be at line 12. And I have no idea what you're trying to do at line 13.
First learn how to structure your conditionals before taking shortcuts by not using curly braces for single-line conditionals.
if ( someCondition == true )
{
// use curly braces until you know what's going on
}
else
{
// code will be confusing if you don't understand the rules
}
in the above example there is no semicolon after the if. SO your update should look something like :
function Update () {
if ( Input.GetAxis("Vertical") ) // no semicolon here
{
animation.CrossFade("Walk_"); // what is this? = walkSpeed;
{
else
{
animation.CrossFade("Rest");
{
}
Thanks! The walkSpeed was the variable for how fast the dragon would move...
and it still says: Unexpected token: else.
Am I supposed to use something other than 'else' in this version of Unity?
fixing alucard -
function Update(){
if(Input.GetAxis("Vertical"))
{
animation.CrossFade("Walk_");
}
else
{
animation.CrossFade("Rest");
}
}
ah, bugger, I did copy-paste those curly braces in the second code example. Thanks for pointing that out DryTear. You can see in the example I did display this properly (unedited I swear). So I shall leave my error and explain it here : yes I did stuff up with the OPs script, but the example at the top does clearly show opening and closing brackets, which just proves my point. $$anonymous$$ake sure you know what is going on ! Every open bracket must have a partner closing bracket. I have made some videos on basic scripting. I think it would be worthwhile for the OP to check them out : http://forum.unity3d.com/threads/173876-Program$$anonymous$$g-and-$$anonymous$$aking-Games-with-Unity-3D . Or the direct link to the conditionals video : http://youtu.be/EhS$$anonymous$$h$$anonymous$$-GIYU
Edit : Upvote to Benproductions1 and DryTear for their answers =]
Edit 2 : I was showing curly braces to indicate how the conditional should be set up. The first problem was the semicolon after the if argument before the outcome, the second problem was indeed the incorrect use of the CrossFade command.
Answer by Benproductions1 · Mar 18, 2013 at 03:12 AM
Ok, so since this has been answered in the comments and I don't have the privileges to convert comments to answers, I will post my own.
The problem you had, was that you were using the wrong syntax for and if statement. Like in many other languages, javascript and therefore unityscript uses {}
curlybrackets for code wrapping, including an if-else statement. Also present in many other languages is the usage of a ;
semicolen for EOL (end of line), this is used for every line of code that is executed individually.
Therefore your code should look like this:
function Update() {
if (/*Your expression for the if statement*/) {
//code here
}
else {
//code here
}
}
You can also stack an if
on top of an else
:
if (/*your expression here*/) {
//Your code
}
else if (/*Your second expression here) {
//Your code
}
else {
//Your code
}
To use an or
expression, in javascript and unityscript, you use ||
So if I want my code to happen when either a
or b
are true
:
true || true //evaluates as true
true || false //evaluates as true
false || false //evaluates as false
false || (true || false) //evaluates as true
And is done similarly with &&
true && true //evaluates as true
true && false //evaluates as false
false && false //evaluates as false
true && (true || false) //evaluates as true
You can also allways use not, which inverts the evaluation and is done using a !
!true //evaluates as false
!false //evaluates as true
!(true && true) //evaluates as false
!(true && false) //evaluates as true
!(false && false) //evaluates as true
I suggest learning these basics before starting anything big :)
Hope this helps, Benproductions1
Thanks guys this was really helpful! Of course, now I have another question about if statements, but I guess that's what this forum is for
Your answer
![](https://koobas.hobune.stream/wayback/20220613094527im_/https://answers.unity.com/themes/thub/images/avi.jpg)