- Home /
IF - ELSE Issue! (Help! I'm new to this)
Hey all, I'm just learning how to script using javascript for Unity. I've just started and learnt a bit about if and else statements, however I am pulling my hair out of frustration now able to work out an error I keep having. I've been through a lot of answers on this website for the trouble I'm having and nothing seems to be helping. I keep getting
'BCE0044: expecting }, found 'else'.
Here are two scripts I wrote, A) gives me errors, B) doesn't. Sorry if this is an easy fix, I have researched and I cannot find any answers as to what is wrong with my script.
Script A) (Gives me error codes)
var pickupKey = true;
var openDoor = false;
var numberOfKeys = 1;
var keysAmount = 0;
function Start (){
}
function Update (){
numberOfKeys = numberOfKeys + 1;
keysAmount = numberOfKeys;
print (numberOfKeys);
numberOfKeys = numberOfKeys + 1;
keysAmount = numberOfKeys;
print (numberOfKeys);
numberOfKeys ++;
print (numberOfKeys);
keysAmount = numberOfKeys;
print ("You have " + keysAmount + " keys in your bag!");
print ("Because of this....");
if (keysAmount >= 3);
{
print ("The door has now opened!");
openDoor = true;
numberOfKeys -= 3;
print ("Now you have " + numberOfKeys + " keys");
}
else
{
print ("Not enough keys to open the door");
}
}
SCRIPT B) (No error codes but I don't see anything I did any different)
function Start () {
}
function Update()
{
var luckyNumber = 6;
if (luckyNumber == 5)
{
print ("Lucky number equals 5");
}
else
{
print ("Lucky number does NOT equal 5");
}
}
Thank you for your time and sorry if this was a bit hard to read, this is my first time using this website so I was a bit unsure how to format this post to make it look cleaner. Please tell me what I've been doing wrong.
Because you are new around here, I formatted your question to give the community half a chance at helping you. Please watch the video that $$anonymous$$ches you how to use the site.
Answer by Graham-Dunnett · Sep 25, 2013 at 11:23 AM
Remove the semi-colon at the end of line 24.
Thanks for the quick replies, and thanks for restructuring my post Graham, I'll check out the video for future posts. Also, removing the semi-colon at the end of line 24 just added another error message which was to put it back on.
I can avoid it for now if I have to, after following a tutorial it was said it was a fundamental so I was trying to learn everything I could from it (from walkerboystudio.com)
It just seems like a really good feature to learn before I move ahead with more complex stuff.
As he said, remove semi-colon on this line if (keysAmount >= 3);
. Tested in Unity and there are no more errors. Tip: double click on the error in the debug window and it will take you to the area of code with mistake.
Thank you guys for all your help, hamstar yours was right, this fixed it right up and now I can see the mistake I made. Thanks again!